2012-09-28 3 views
0

누군가가 mysql 카운터를 재설정하는 데 도움을 줄 수 있습니까? 당신이 도달하면 그 자체에5에 도달했을 때 카운터를 재설정하는 방법

$counter=0; 

카운터 추가하여 업데이트 쿼리에 0 값을 한 후, 가장 가능성이 변수되지 않은 : 여기 당신이 $를 잊어

$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 

답변

0

이것은 corrct 작업 코드입니다.

<?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(); 
?> 
7

코드입니다 5

If ($counter==5){ 
    echo "counter=5 "; 
    $counter=0; 
    $addcounter=0; 
} 
+0

시도했지만 오류는 제거했지만 다시 설정하지 않았습니다. 어떤 생각이든 – Ree

+0

에 오류가 있습니까? – Miroslav

+0

오류가 없지만 이후에 재설정하지 않습니다. – Ree

관련 문제