Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in MySQL UPDATE (PHP/MySQL)

I am using this code so I can update a record in database:

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

My question is: How can I use variables in a MySQL UPDATE statement.

like image 800
MU_FAM Avatar asked Dec 03 '22 02:12

MU_FAM


1 Answers

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

You was messing up the quotes and concats.

You can use inline vars like the previous example or concat them like:

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

like image 88
morgar Avatar answered Dec 11 '22 09:12

morgar