I’ve finally found why my PHP script didn’t work. It was because of MySQL “–comment” instead of “– comment”. I recently started with PHP, and until then, I had always used “–comment”. Now, I wonder why this isn’t allowed in PHP’s mysql_query()?
<?php
$query = "SELECT firstname,
-- comment
lastname, address,
--not a comment
age FROM friends WHERE firstname='%s' AND lastname='%s'";
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
?>
Per MySQL docs a space is required. Also, as stated on the previous linked page, MySQL deviates from the spec in this syntax (which is just 2 consecutive dashes [
--this is a comment]).