p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 (http://p2p.wrox.com/forumdisplay.php?f=476)
-   -   how do i check if query is empty? (http://p2p.wrox.com/showthread.php?t=86686)

gkman February 15th, 2012 02:08 PM

how do i check if query is empty?
 
Hey everyone.

I am trying to check if my query returned any row of info.

I tried using the empty() function - fail.
I guess this is because my $result isnt a empty variable- it contains an empty query.

I tried to use the mysql_num_rows()==0 function - also no good.
This is because it is possible that my query will return only one row of info (and it does) which will cause the above function to return 0.

I did try:

Code:

$result = mysql_fetch_array($result)
if ( mysql_num_rows($result)==0 && $result[0] = ' ')

That seemed to worke but it doesn't seem right to me because it is possible to have an empty block of info, And this condition won't be good for that.

So how do you check if the query is empty?

UseLess February 16th, 2012 08:08 AM

Greetings,

Code:

$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
        // $result is false and this indicates
        // the query failed so output a message
        // optionally stop processing script here
}
else
{
        // query was successful
        // so start a loop to grab each row from the database
        // the loop will end when there are no more rows to be returned
        // in this case $row will be false
        while( $row = mysql_fetch_array($result);
        {
                // do something with the data
        }
}

Another way to do the above but this time testing to see if anything is returned

Code:

$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
        // $result is false and this indicates
        // the query failed so output a message
        // optionally stop processing script here
}

// test to see if anything was returned by the query
if( $row = mysql_fetch_array($result) )
{
        // db returned something so process it
        do
        {
                // process data
                // data will be in the $row variable used above
               
                // do while loops run once as the test is done below to determine
                // if the loop should actually loop
        }
        while( $row = mysql_fetch_array($result) );
}
else
{
        // optional
        // display message that nothing was returned from db
}


gkman February 17th, 2012 08:32 AM

I just tried Both of your suggestion- none of them worked.
I think I may have a problem with my installed php because suddenly the mysql_num_rows() function does the trick and the php stopped handling errors

UseLess February 18th, 2012 07:27 AM

Greetings,

In your example above you're using this;
Code:

$result = mysql_fetch_array($result)
if ( mysql_num_rows($result)==0 && $result[0] = ' ')

So assuming you're putting the result of the query into $result you cannot then use;
Code:

$result = mysql_fetch_array($result);
as you have just destroyed the variable holding the db pointer to the query result. So if there is more than one row to be returned from the db you won't get them as you have just destroyed the only way to access them.

Therefore in this case the result of the 'mysql_num_rows' call could be meaningless due to destroying the valid db pointer and turning it into an array instead.

So basically get the result of the query into a variable and then each row from the db into another variable....


All times are GMT -4. The time now is 10:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.