Jump to content

[SOLVED] an easy one I think!??!


izbryte

Recommended Posts

I have this query and I'm trying to echo some text if there are no results but it's not working. I'm sure this is either me being a bonehead or it's been WAY too long since I took my last mySQL class... LOL

 

Here's what I have:

$query = "SELECT * from firms ORDER BY firmID DESC"; 
$result = mysql_query($query);
if ($result){		
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
		echo (whatever the results were);
               }
}
else{
echo "No results found.";
}

My results are coming up fine but if it's empty I just see blank....

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/
Share on other sites

$result will return true whether or not any rows was returned or not. What you should use is mysql_num_rows to see if any rows was returned or not, ge:

$query = "SELECT * from firms ORDER BY firmID DESC";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0)
{
echo "No results found.";
}
else
{
    while($row = mysql_fetch_assoc($result))
{
    echo (whatever the results were);
}
}

<?php
$query = "SELECT * FROM `firms` ORDER BY firmid DESC";
$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_assoc($result)){
	$text = (strlen($row['field']) > 0) ? "{$row['field']}<br>" : "";
	echo $text;
}
}else {
echo "There are no firms available";
}
?>

<?php
$query = "SELECT * from firms ORDER BY firmID DESC"; 
$result = mysql_query($query);
$noresults = mysql_num_rows($result);
if ($noresults != 0){		
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
		echo (whatever the results were);
               }
}
else{
echo "No results found.";
}
?>

$query = "SELECT * from firms ORDER BY firmID DESC"; 
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
	$row = mysql_fetch_array($result)
	while($row )		{
		echo (whatever the results were);
      }
}
else {
echo "No results found.";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.