Question:
PHP/SQL question: When doing an SQL query and there are no results, how do I output a "no results" message?
2007-08-17 16:23:41 UTC
I am php/sql coder in training, trying to teach myself as required. I may not know some of the core principals of sql, but I am coming along.

Specifically, I am doing a query based on an "if" statement, such as below:

if($nameresult!="")
$query = ("SELECT * FROM $table WHERE name='$nameresult'");

$nameresult being the post results from a form on the page this code is on. Noticed the "!=" in the statement. If the field is not blank, it will do the query.

Now, what I would like to have, is when the name typed into the field is not found in the database, it will return a custom error printed instead, or it pulls a specific line in my database that has "No results" for a name, which is easy enough to query for. How do I specify/acknowledge the condition of an empty result and act on it?

Sorry for the newbie question, if the answer is obvious to some.
Five answers:
2007-08-17 21:33:06 UTC
As the PHP documentation notes, mysql_num_rows() does not return an empty string, it returns an integer; 0 when there are no rows, or false when there is an error.



So, if you use the identity operator (===), you will always be able to test against 0 (since 0 and false are the same thing).



if( mysql_num_rows($result) === 0 ) {

// code if there are no rows

}

else {

//code if there are rows

}



Testing against an empty string is not clean because it requires PHP to implicitly cast a zero or false in order to make a comparison.
freakazoidic
2007-08-17 16:47:39 UTC
I'm not going to do it for you, but I can help you find the correct logic.



Check out this link: http://us3.php.net/mysql-num-rows



The num_rows function is used to find out number of results a query finds.



Now, what you want to do is have them search for whatever it is they are searching for, use the num_rows to find out how many results were found.



Next you want to create a conditional statement (if else) that will check the value of num_rows. If it finds NO RESULTS, then you echo whatever statement you want. If it finds results, then you show those results.



If you search around the web, I'm SURE you can find a great tutorial on something like this. http://www.webmonkey.com is a great place to start also. You might even try http://www.sitepoint.com.
2016-04-02 06:49:37 UTC
I have been in Angels Stadium, Dodger Stadium, Yankee Stadium, Petco Park and whatever the name is of that sauna in Miami in Red Sox gear. Everyone treats you with respect/ignores you, except the Yankee fans. There is just too much history there. It makes it fun. As for your experience, I could never understand why a Yankee fan would show up at Fenway for a Red Sox--Tigers game in Yankee gear. Wear Tiger gear for Heaven's sake. Of course you are going to be the most loathed person in the park. Actually wearing a Yankee hat in the City of Boston is not the brightest of moves as a general matter.
Big D
2007-08-17 17:48:50 UTC
if( !empty($nameresult) ){



$query = ("SELECT * FROM $table WHERE name='$nameresult'");



if($result = mysql_query($query)){

while($row = mysql_fetch_array($results)){

//get your data



}

}



if(mysql_num_rows($rowAmount) == 0){

print 'There are no results for this query';

}



}
johnny_bravo_18ph
2007-08-18 05:23:25 UTC
try if(empty($query_result)) { echo "no results"; }

or try this if(is_null($query_result)) { echo "no results"; }













-- http://www.01-designs.com - website design templates


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...