$names = array();
$names['full-name'] = 'John Doe';
$query = "INSERT INTO users (full_name) VALUES ('$names[full-name]')";
$result = mysql_query($query) or die(mysql_error());
I have been getting syntax errors whenever I attempt to create a mysql query using an array key that contains a hyphen:
Parse error: syntax error, unexpected ‘-‘, expecting ‘]’
How am I able to fix this? Thank you.
Since it hasn’t been mentioned yet, here’s an another alternative syntax:
Adding the
{}braces around the variable/array index allows you to quote it as you would if it were outside of the string. The{}notation also allows you to embed a multi-dimensional array inside a string, e.g.would normally be see as array
$x[1]followed by literal[2]text by PHP, since its array parsing is not ‘greedy’. But putting braces aroudn the whole array reference forces PHP parse the entire thing.