PHP / MYSQL error: mysql_num_rows(): supplied argument is not a valid [closed]

雨燕双飞 提交于 2019-12-12 01:47:51

问题


I have the following code:

$sql = mysql_query("SELECT id FROM logins WHERE id='" . $this->skID . "'");

if(mysql_num_rows($sql) == 0) { return false; }
else {
      list($skID) = mysql_fetch_row($sql);
      return $skID;
}    

which brings back the below error.

mysql_num_rows(): supplied argument is not a valid MySQL result resource

I have echoed out the SQL and run this in the database and I get a result, so what the problem - any ideas most welcome?

[EDIT] Sorry Im being an absolute idiot, trying to use mysql functions on an ms database! Sorry!


回答1:


have you checked that your connection has been established. perhaps try outputting the query results after your query. also try to echo your mysql_error() to see if anything went wrong.

echo mysql_error();

straight after your query.




回答2:


a correct way of running queries (along with consistent variable naming):

$sql    = "SELECT id FROM logins WHERE id='" . (int)$this->skID . "'";
$result = mysql_query($sql) or trigger_error(mysql_error." in ".$sql);

this code will tell you what is wrong with your query.

using die() is not recommended as it will break your code execution and make the page looks messy. not to mention that unconditional output of the error message to the screen is a security flaw.




回答3:


Change

$sql = mysql_query("SELECT id FROM logins WHERE id='" . $this->skID . "'");

to

if (!$sql = mysql_query("SELECT id FROM logins WHERE id='" . $this->skID . "'")) {
  // handle error here
  // for example:
  die('MySQL Error: '.mysql_error());
  // ...but don't show the result of mysql_error() in a production environment!!
}

You query is failing, mysql_query() returns FALSE, and mysql_num_rows() expects a result resource, not a boolean, so it emits the error you are seeing.




回答4:


Your query is failing. Check why with mysql_errno

http://us2.php.net/mysql_errno



来源:https://stackoverflow.com/questions/7995001/php-mysql-error-mysql-num-rows-supplied-argument-is-not-a-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!