mysql_result not valid resourced
this is my code
function code_exists($code){
$code = mysql_real_escape_string($code);
$code_exists = mysql_query("SELECT COUNT('url_id') FROM 'links' WHERE 'code'='$code'");
return (mysql_result($code_exists, 0) == 1) ? true : false;
}
it is supposed to check the db to make sure the $code is in it or not but it keeps giving me the same error
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
/home/codyl/public_html/proje开发者_运维百科cts/tests/url/func.inc.php on line 16
You have syntax error in mysql query try this.
They are not single quote they are backticks `
mysql_query("SELECT COUNT(`url_id`) FROM `links` WHERE `code`='$code'");
I would recommend you should always handle mysql errors.
you can rewrite like this
mysql_query("SELECT COUNT(`url_id`) FROM `links` WHERE `code`='$code'")
OR
trigger_error(mysql_error());
As being said by Shakti you have an error in your sql
$code_exists = mysql_query("SELECT COUNT(`url_id`) FROM `links` WHERE `code`='$code'");
You need to surround field name with ticks (`) not single quotes. You should also add a statement to chech if the query was run as expected.
$code_exists = mysql_query("SELECT COUNT(`url_id`) FROM `links` WHERE `code`='$code'") or die('Error in my query: '. mysql_error());
This will kill your program throwing the mysql error.
You are not catching errors coming from mySQL when the query fails. Use mysql_error()
to output them, e.g. like so:
$code_exists = mysql_query(....);
if (!$code_exists)
trigger_error("MySQL error: ".mysql_error(), E_USER_ERROR);
(Using trigger_error()
is so when your site goes live, and error reporting is turned off, mySQL errors are not visible to the user.)
In this specific instance, using single quotes for table names is invalid so there will have been a mySQL syntax error.
You need to either use backticks ` or no quoting at all.
精彩评论