Hi,

I get this error on red coloured line. "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\.....".
I use PHP 5.25, MySQL 5.51.

$sql="SELECT * FROM customer";
$run = mysql_query($sql);	
    if (mysql_num_rows($run) > 0) {
    .... 
    }

When there is record it works otherwise no.

Thanks

Recommended Answers

All 6 Replies

You have at least two options:

#1 quick and dirty:

replace the line with this:

if (@mysql_num_rows($run) > 0)

#2 Check the return type.

if (empty($run)) {
    if (mysql_num_rows($run) > 0) {
    .... 
    }
} else {
   // NO results
}

a quote from http://www.php.net/mysql_query

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

What this means is that something is wrong with the query. To find out what, you should connect a die statement to your mysql query function.

$run = mysql_query($sql) or die(mysql_error());

Keep in mind that an empty result set is still a result set so if mysql_num_rows($run) == 0 then it is still a valid "MySQL result resource".

To find out what, you should connect a die statement to your mysql query function.

$run = mysql_query($sql) or die(mysql_error());

This is recommended only if you are troubleshooting on your local machine.

A suggestion like that should NEVER be used on a production server because the error may contain *interesting* information that can be used by bad people.

This is not against R0bb0b personally, but against his/her suggestion.

Hi,

I have used,

if (@mysql_num_rows($runSql) > 0)

It works fine.

Thanks guys

This is recommended only if you are troubleshooting on your local machine.

Yes, it is common sense not to display php/mysql errors publicly. But doing it long enough just to get a database error vs opening your db manager, verifying that you are in fact using the correct database on the correct server, inserting test data into into the query(assuming that the test data that you insert correctly resembles the variables in your query) and running it to find the error.
hmm:-/

Hi,

I have used,

if (@mysql_num_rows($runSql) > 0)

It works fine.

Thanks guys

Cool :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.