Forum Moderators: coopster

Message Too Old, No Replies

verify query return without num_rows

         

dkin

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Administrator 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
}