homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

verify query return without num_rows

 1:44 am on Apr 19, 2006 (gmt 0)

I would like to run this code.

// Make a MySQL Connection

$query = "SELECT name FROM owned where type = 'Breastplate'and uid = $uid";
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo '<option value="'. $row['name'] .'">'. $row['name'].'</option>';

but without using mysql_num_rows and preferrably without using lengthy if statements I would like to verify that the sql actually returns some rows.

Anyone know?



 6:34 am on Apr 19, 2006 (gmt 0)

You can try mysql_num_rows. It returns the number of rows affected by your query.
So a simple check (>1) should be enough


 11:20 am on Apr 19, 2006 (gmt 0)

Another option is to initialize a variable and instead of printing the data on each iteration of the loop, concatentate it to the existing variable. At the end of the loop processing you can see if it is still empty:
$mydata = ''; // initialize 
while($row = mysql_fetch_array($result)){
//echo '<option value="'. $row['name'] .'">'. $row['name'].'</option>';
$mydata .= '<option value="'. $row['name'] .'">'. $row['name'].'</option>';
if ($mydata) {
// yes, we processed some rows

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved