MU_FAM
MU_FAM

Reputation: 79

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.

Upvotes: 5

Views: 81789

Answers (3)

Fahim Faysal
Fahim Faysal

Reputation: 459

Use apostrophes when using variables in a MySQL UPDATE statement:

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

Be careful about space and apostrophes.

Upvotes: 2

dee-see
dee-see

Reputation: 24078

You messed up on your " . pattern.

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

Upvotes: 4

morgar
morgar

Reputation: 2407

$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);

Upvotes: 13

Related Questions