homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
dkin

10+ Year Member



 
Msg#: 12567 posted 1:44 am on Apr 19, 2006 (gmt 0)

I would like to run this code.

<?php
// 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?

 

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 12567 posted 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

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 12567 posted 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