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
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);
}
}

Link to comment
Share on other sites

<?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";
}
?>

Link to comment
Share on other sites

$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.";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.