[SOLVED] How to detect no results from mysql_query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

[SOLVED] How to detect no results from mysql_query?

Post by charp »

Okay. Nuff of the banging my head on the table and the fruitless searches on the net for my answer. Help me...

How do I determine if the following query returns nothing? I want to write a conditional statement that if the query returns nothing, then do something else.

Code: Select all

$results = mysql_query("SELECT last_name FROM users WHERE last_name LIKE '$lastinitial%' ORDER BY last_name ASC");
Be kind to the PHP newbie. :wink:
Last edited by charp on Sun Dec 28, 2003 10:30 am, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

After you assign the query to the variable result, then you could assign the number of rows returned to another variable called 'rowsReturned' using the php function mysql_num_rows() and then check to see if 'rowsReturned' is more than 0.

Example;

Code: Select all

<?php
// Assume result already contains a query
$rowsReturned = @mysql_num_rows($result); // Use the @ symbol to suppress the errors of a function
if ($rowsReturned > 0) { // rows were returned }
else { // no rows returned by query }
?>
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

Thank you, nigma, Thank you! :)

I tried many variations similar to your code, but nothing seemed to work for me. So, I'm wondering if it has anything to do with the @ symbol in front of the function???? That little trick is not one I've used before.

Can anyone shed a little light on when the @ symbol should be used and when it shouldn't?

----------------------
Said it before -- gotta say it again -- this forum is tops!
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

You can preface a (builtin php ?) function call with an @ sign, as in:
@foo();
This will supress the injection of error messages into the data stream output to the web client. You might do this, for example, to supress the display of error messages were foo() a database function and the database server was down. However, you're probably better off using php configuration directives or error handling functions than using this feature.
See the section on error handling functions.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I usually end up doing something like this:

Code: Select all

@mysql_connect(HOST, USER, PASS) or error("Connection to database server failed.");
By doing that the visitor doesn't get a non-understandable error message from the mysql_connect() function, but both the visitor and I know that there is a problem. From there I can remove the @ symbol temporarily to discover why the connection attempt failed.

Also, Charp, make sure that whatever variable you assign the query to is the same variable that you give mysql_num_rows() as an argument.

Example:

Code: Select all

<?php
$result = mysql_query("select column1, column2 from tablename");
$rowsReturned = @mysql_num_rows($result);
if ($rowsReturned > 0) { // One or more rows were returned }
else { // No rows were returned }
?>
Post Reply