Welcome to WebmasterWorld Guest from 23.22.140.143

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)

Preferred Member

10+ Year Member

joined:Mar 4, 2004
posts:553
votes: 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?

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
posts:941
votes: 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)

Administrator

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

joined:July 31, 2003
posts:12533
votes: 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
}
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members