php - Use of ' in SQL statement -
i doing php; found following code works
function get_total_urls() { $total = mysql_query('select count(`url_key`) `urls`'); return (int)mysql_result($total, 0); } get_total_urls();
but following code shows warning
function get_total_urls() { $total = mysql_query("select count('url_key') 'urls'"); return (int)mysql_result($total, 0); } get_total_urls();
shows following warning:
warning: mysql_num_rows() expects parameter 1 resource, boolean given.
please help; why 2nd code shows warning 1st code ok? 2nd code fails count of 'url_key' database table.
is there anyway write first code without back-tick , work perfectly?
this statement:
$total = mysql_query('select count(`url_key`) `urls`');
in mysql, backticks used delimit identifiers -- when conflict reserved words or contain unseemly characters (such spaces, periods, commas, , on). because idea have identifiers not conflict reserved words , contain no unseemly characters, backticks needed.
you can write:
$total = mysql_query('select count(url_key) urls');
single quotes should only used string , date constants. not use them identifiers, lead confusion. have no doubt noticed.
Comments
Post a Comment