RE: [PHP-DB] If select query doesn't net any results
You're absolutely right. It must be the 1.5 hours of sleep last night that made my brain shrivel up like that. 8( Doug On Thu, 9 Jan 2003 17:45:00 -0500, John W. Holmes wrote: >> ... >> if ($result) { >> while ($row... >> ... >> } >> >> $result will be 0 (false) if nothing satisfies the query. Any other >> comparison is just extra typing with no improvement in logic. > >No, it only returns false/0 if the query fails, meaning there was an >error and the query couldn't be executed. The query can execute just >fine, return no rows, and so the result would be true. > >What you have is extra typing with no improvement in logic. ;) > >If you need to know the number of rows returned, then use the >if(mysql_num_rows() method. If you don't, then you can do this: > >if($row = mysql_fetch_row($result)) >{ > do{ > //do whatever with $row data > }while($row = mysql_fetch_row($result)); >} >else >{ echo "no rows returned from query"; } > >If you simply do not want something done if no rows are returned, then >the simple while($row = mysql_fetch_row($result)) method works fine, as >the while() will only be true if rows were returned and skipped if no >rows are returned. > >---John W. Holmes... > >PHP Architect - A monthly magazine for PHP Professionals. Get your copy >today. http://www.phparch.com/ > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] If select query doesn't net any results
> ... > if ($result) { > while ($row... > ... > } > > $result will be 0 (false) if nothing satisfies the query. Any other > comparison is just extra typing with no improvement in logic. No, it only returns false/0 if the query fails, meaning there was an error and the query couldn't be executed. The query can execute just fine, return no rows, and so the result would be true. What you have is extra typing with no improvement in logic. ;) If you need to know the number of rows returned, then use the if(mysql_num_rows() method. If you don't, then you can do this: if($row = mysql_fetch_row($result)) { do{ //do whatever with $row data }while($row = mysql_fetch_row($result)); } else { echo "no rows returned from query"; } If you simply do not want something done if no rows are returned, then the simple while($row = mysql_fetch_row($result)) method works fine, as the while() will only be true if rows were returned and skipped if no rows are returned. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] If select query doesn't net any results
... if ($result) { while ($row... ... } $result will be 0 (false) if nothing satisfies the query. Any other comparison is just extra typing with no improvement in logic. Regards, Doug On Thu, 09 Jan 2003 13:06:51 -0500, Michael Cortes wrote: >I am a programming novice and just created my first php application. However I have >a situation I >don't know the answer to. > > >My db queries (mysql) are written like this: > > $qtrans= "select * from $table where (conditionals) order by some field"; > > $link=mysql_connect ($host, $user, $password); > > $result=mysql_db_query($dbname, $qtrans, $link); > > while ($row = mysql_fetch_array($result)) { > > do stuff with the array here > > } > > > >I would like to created an if statement that only occurs if the query gets a result. >If there are >no records that meet the query conditionals and my array ends up with null, I do not >want my if >statement to take place. > >Is this easy enough to do? Any help would be great. > >Thank you, > > > > >Michael Cortes >Fort LeBoeuf School District >34 East 9th Street >PO Box 810 >Waterford PA 16411-0810 >814.796.4795 >Fax1 814.796.3358 >Fax2 978-389-1258 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] If select query doesn't net any results
> I would like to created an if statement that only occurs if the query gets a result. > If there are > no records that meet the query conditionals and my array ends up with null, I do not >want my if > statement to take place. > Is this easy enough to do? Any help would be great. if( mysql_num_rows( $result ) > 0 ) { // Records were returned // Do something here // } Chris -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] If select query doesn't net any results
if (mysql_num_rows($result) < 1) { //do stuff } This is only this easy with mySQL... :-) <>< Ryan -Original Message- From: Michael Cortes [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 11:07 AM To: [EMAIL PROTECTED] Subject: [PHP-DB] If select query doesn't net any results I am a programming novice and just created my first php application. However I have a situation I don't know the answer to. My db queries (mysql) are written like this: $qtrans= "select * from $table where (conditionals) order by some field"; $link=mysql_connect ($host, $user, $password); $result=mysql_db_query($dbname, $qtrans, $link); while ($row = mysql_fetch_array($result)) { do stuff with the array here } I would like to created an if statement that only occurs if the query gets a result. If there are no records that meet the query conditionals and my array ends up with null, I do not want my if statement to take place. Is this easy enough to do? Any help would be great. Thank you, Michael Cortes Fort LeBoeuf School District 34 East 9th Street PO Box 810 Waterford PA 16411-0810 814.796.4795 Fax1 814.796.3358 Fax2 978-389-1258 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php