Why isn't "--comment" working when "-- comment" does?
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 q开发者_JS百科uery: ' . 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
]).
MySQL comments beginning with --
must be followed by a space in order to be considered a comment. This is something specific to MySQL, and has nothing to do with PHP. You may also use /* comment */
or # comment #
From the manual: the “-- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on).
Because the MySQL dev team decided that if SELECT -1
is valid code to select -1
then SELECT --1
Should be valid (My)SQL for selecting +1.
Because a comment starts with three characters: dash, dash, space. "-- "
精彩评论