Forum Moderators: coopster

Message Too Old, No Replies

Syntax trouble with MySQL query and array variable?

         

endtheme

9:48 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



I am getting an error when I try to use a variable array in a mysql query.

I guess the problem is I don't know the syntax for including a variable array into a mysql_query request. The quotation marks used in the array muck things up.

Here is some pseudo-code to show the problem...

$array['name'] = 'John Doe';

mysql_query("DELETE from table WHERE name = $array['name']");

So how would I properly write that?

I've tried

mysql_query("DELETE from table WHERE name = '$array['name']'"); // Doesn't work
mysql_query("DELETE from table WHERE name = " . $array['name'] . ""); // Doesn't work

cameraman

11:01 pm on Jul 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To refer to an array inside a string you need braces, and to make mysql happy you need single quotes:
mysql_query("DELETE from table WHERE name = '{$array['name']}'");