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
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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.