0

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!

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
LeeTee
  • 6,401
  • 16
  • 79
  • 139
  • Hint: when you figure something like that - then do the reasonable thing and **immediately** delete your question. Leaving it around will mean that other people will spent their time looking into close requests! – GhostCat Oct 10 '16 at 14:16

4 Answers4

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.

Luc
  • 985
  • 7
  • 10
1

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

Your query is failing. Check why with mysql_errno

http://us2.php.net/mysql_errno

nickb
  • 59,313
  • 13
  • 108
  • 143