Jump to content

Warning: mysql_num_rows():


MasterACE14

Recommended Posts

morning,

 

I have a basic check for how many rows there are in a query and its throwing me this error.

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ace/public_html/conflictingforces/lib/messages.php on line 28

 

here's my code(relevant code only):

 

<?php

// the query
$query = mysql_query("SELECT * FROM `cf_messages` WHERE owner='$player[id]' ORDER BY date DESC LIMIT 50");

// line 28 , the rows
if (mysql_num_rows($query) == 0)

?>

 

any help is greatly appreciated.  :)

 

Regards ACE

 

Link to comment
https://forums.phpfreaks.com/topic/102621-warning-mysql_num_rows/
Share on other sites

$query = mysql_query("SELECT * FROM cf_messages WHERE owner='{$player['id']}' ORDER BY date DESC LIMIT 50");

 

// line 28 , the rows

if (mysql_num_rows($query) == 0)

 

You need to use {} around arrays when echoing them or using them in double quotes.  And DON'T USE ` FOR COLUMN NAMES.  Thanks. =)

Debugging hint #1

 

Put the query into a $var before calling mysql_query()

$sql = "SELECT * FROM `cf_messages` WHERE owner='$player[id]' ORDER BY date DESC LIMIT 50";

 

Debugging hint #2

 

get the content of mysql_error() to tell you why it failed

 

$res = mysql_query ($sql) or die (mysql_error() , ' in '  . $sql);

You should always check that mysql_query() returned a result resource (this is not the same as returning rows) prior to trying to use it. eg;

 

<?php

  $sql = "SELECT foo FROM bar";
  if ($result = mysql_query($sql)) {
    // query worked, check for rows.
    if (mysql_num_rows($result)) {
      // rows found, display data.
    } else {
      // no rows found.
    }
  } else {
    // query failed, handle error.
  }

?>

You should always check that mysql_query() returned a result resource (this is not the same as returning rows) prior to trying to use it. eg;

 

<?php

  $sql = "SELECT foo FROM bar";
  if ($result = mysql_query($sql)) {
    // query worked, check for rows.
    if (mysql_num_rows($result)) {
      // rows found, display data.
    } else {
      // no rows found.
    }
  } else {
    // query failed, handle error.
  }

?>

 

ok, I will do that from now on, thanks  :)

 

I added you to MSN. =P

 

excellant  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.