2015-04-07 67 views
0

我嘗試製作一個簡單的頁面查看計數器。 每次頁面刷新時,數字應增加+1。Php mysql頁面查看計數器

當我在Phpmyadmin中執行代碼時,它一切正常。但在PHP中,計數器返回,但不增加。我做錯了什麼?

<?php 
$username = "username"; 
$password = "password"; 
$hostname = "hostname"; 
$dbname = "dbname"; 


// Create connection 
$conn = new mysqli($hostname, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 

$sql = "UPDATE `Games` SET `Played`= `Played`+1 WHERE 'ID'='3'"; 

$sql = "SELECT `Played` FROM `Games` WHERE `ID` = 3"; 

$result = mysqli_query($conn, $sql); 

if (mysqli_num_rows($result) > 0) { 
// output data of each row 
while($row = mysqli_fetch_assoc($result)) { 
    echo "" . $row[Played]. "<br>"; 
} 
} else { 
echo "0 results"; 
} 

$conn->close(); 
?> 
+2

這是一個錯誤? ''密碼;'<=缺少引號,另外,這是一個'WHERE'ID''不是數值的列 –

+0

刪除''ID''上的單引號 – Daan

+0

'$ result = mysqli_query($ conn,$ sql)或者死(mysqli_error($ conn));'會發出語法錯誤信號 –

回答

1

您必須創建更新串入$sql變量之後做出mysqli_query電話。你現在正在做的只是用SELECT查詢字符串覆蓋它。

[...] 

$sql = "UPDATE `Games` SET `Played`= `Played`+1 WHERE `ID`='3'"; 

$result = mysqli_query($conn, $sql); 

$sql = "SELECT `Played` FROM `Games` WHERE `ID` = 3"; 

$result = mysqli_query($conn, $sql); 

[...] 
+1

您忘了'WHERE'ID'='3'' – TiiJ7

+0

就是這樣,非常感謝! – Tim

+1

它應該反映出答案^'WHERE'ID'='3'「;'爲'WHERE ID ='3'」;'或者使用滴答聲''''而不是普通引號''',請編輯您的答案。不正確的標識符http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html –