Jump to content

check to see if query result exists


mattVo

Recommended Posts

Hi all,

This is probably really easy, but I was wondering how to check if a query result exists. If it does then echo it, if not, check again until the query results are returned.

I'm asking this because my pages get frequently displayed even when no query results exist, resulting in a page without content.

 

THanks all,

Matt

Link to comment
https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/
Share on other sites

<?php

/*
* db connection omitted
* all error checking ommited
* basicaly you only have what you asked for
*/

$sql = "SELECT * FROM `somewhere`";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
//you have results
} else {
//you don't have results
}

Depending on your situation it's sometimes useful just to make a function like user_exists(), you could input a username and it would return true or false, the core of that function could look like:

$result = create_query('some query..');
if(!mysql_num_rows($result))
{
return false;
}
return true;

 

Thanks for the quick replies,

But I would like to check until I have results, sort of like going through a loop to check. If none exist, go through the loop until I have results, then do something with them.

 

Matt

What do you mean? If a SQL returns no results, then looping it won't do any good. Mind posting up the pertinent code so we may know what you're talking about?

Hope this makes things clearer:

Check to see if SQL results exist, if not, check again and again until they exist. When they do exist, use them.

What do you mean check-again? The results aren't going to change within the execution of the script...

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.