首页 > 数据库 > MySQL > 正文

用PHP和MYSQL建立计数器过程详解

2024-07-24 12:56:37
字体:
来源:转载
供稿:网友

运行mysql.exe
mysql> create database counter;
query ok, 1 row affected (0.10 sec)

mysql> use counter;
database changed

mysql> create table tl(id integer(2),count integer(5));
query ok, 0 rows affected (0.21 sec)

mysql> show columns from tl;
+-------+--------+------+-----+---------+-------+
| field | type   | null | key | default | extra |
+-------+--------+------+-----+---------+-------+
| id    | int(2) | yes  |     | null    |       |
| count | int(5) | yes  |     | null    |       |
+-------+--------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql> select * from tl;
empty set (0.08 sec)

mysql> insert into tl(id,count) values(1,1);
query ok, 1 row affected (0.02 sec)

mysql> select * from tl;
+------+-------+
| id   | count |
+------+-------+
|    1 |     1 |
+------+-------+
1 row in set (0.03 sec)

下面编写html文件内容:


<html>
<body>

<?php
$myvar = "来访人数:";
echo $myvar;
?>

<?php
 mysql_connect() or die("there are problems to connect to mysql!");
 $query="select * from tl";
 $result=mysql_db_query("counter",$query);
 if($result){
  $r=mysql_fetch_array($result);
  $counter=$r["count"];
  $counter=$counter+1;
  echo $counter;
  $query="update tl set count='$counter' where id=1";
  $result=mysql_db_query("counter",$query);
  mysql_close();
 }
?>
</body>
</html>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表