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.
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.
<?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 }
?>
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!
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.
@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.
<?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 }
?>