Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

verify query return without num_rows



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

10+ Year Member

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)

WebmasterWorld Senior Member 10+ Year Member

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)

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

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

Featured Threads

Hot Threads This Week

Hot Threads This Month