2011-04-30 52 views
2

使用變量,我用這個代碼,這樣我就可以更新數據庫中的記錄:在MySQL UPDATE(PHP/MySQL的)

$query = mysql_query("UPDATE article 
         SET com_count = ". $comments_count 
         WHERE article_id = .$art_id "); 

我的問題是:如何使用變量在MySQL UPDATE語句。

回答

9

$query = mysql_query("UPDATE article set com_count = $comments_count WHERE article_id = $art_id");

你被搞亂了報價和concats。

您可以使用內聯瓦爾像以前的例子或CONCAT他們喜歡:

$query = mysql_query("UPDATE article set com_count = " . $comments_count . " WHERE article_id = " . $art_id);

+0

非常感謝你......它的工作完美 – 2011-04-30 01:02:35

+0

不客氣,請標記此答案(或我的,但這是更好的;))如接受,如果它解決了你的問題。 – Vache 2011-04-30 01:04:50

+0

相信我我會但是..它說我的聲望應該超過15 ..並且它不會..忍受男人 – 2011-04-30 01:20:40

1

你有你的格局" .搞砸。在MySQL UPDATE語句中使用變量時

$query = mysql_query("UPDATE article set com_count = ". $comments_count . " WHERE article_id = " . $art_id . "); 
+0

非常感謝你的幫助 – 2011-04-30 01:03:04

0

使用撇號:

$query = mysql_query("UPDATE article 
         SET com_count = '$comments_count' 
         WHERE article_id = '$art_id'"); 

要小心空間和撇號。