Php 达到5时如何重置计数器
有人能帮我重置mysql计数器吗。这是密码Php 达到5时如何重置计数器,php,Php,有人能帮我重置mysql计数器吗。这是密码 $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select database. mysql_connect("
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counter'];
// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// count more value
$addcounter=$counter+1;
// reset counter if 5 has been reached
If (counter==5){
echo "counter=5 ";
// now im getting an error here//
counter=0;
}
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);
mysql_close();
?>
the error is:
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\test\counter.php on line
and based on // counter=0
你忘了美元:
计数器本身不是一个变量,另外,在更新查询中,如果达到5,则最有可能在0之后返回值
If ($counter==5){
echo "counter=5 ";
$counter=0;
$addcounter=0;
}
这是正确的工作代码
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="gametest"; // Database name
$tbl_name="counter"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counter'];
// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// count more value
$addcounter=$counter+1;
// reset counter if 5 has been reached
If ($counter==5){
$counter=0;
$addcounter=0;
}
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);
mysql_close();
?>
我尝试过,它会删除错误,但不会重置。有什么新的想法吗?我怎么看你修改过的帖子?我刚刚修改了回复,你需要重置$addcounter和$counterpress我迷路了,我怎么做。如果你有这段代码,它将非常有用,只要看看我刚刚修改的回复,你的sql查询是update$tbl_name set counter='$addcounter';但您没有将$addcounter重置为0;在我添加的代码中,如果$counter==5,则重置$addcounter=0;
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="gametest"; // Database name
$tbl_name="counter"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counter'];
// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// count more value
$addcounter=$counter+1;
// reset counter if 5 has been reached
If ($counter==5){
$counter=0;
$addcounter=0;
}
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);
mysql_close();
?>