No entries found message

[code]

$data = mysql_query(“SELECT * FROM horses WHERE breed=‘Abaco Barb’ AND gender=‘mare’ AND allowed=‘OK’ ORDER BY points DESC”)
or die(mysql_error()); [/code]

What changes do I need to make to this code if I would like people visiting this page to see a message such as “Sorry, there are no entries for this breed” if there are no applicable rows in my table?

Many thanks.

You can just count the number of rows that are returned.

$num_rows = mysql_num_rows($data); if($num_rows == 0){ echo "Sorry, there are no entries for this breed"; }else{ ........ }

:D Excellent, thank you!

Sadly, experiencing issues:

[code] <?php
mysql_connect("****", “", "”) or die(mysql_error());
mysql_select_db("*****") or die(mysql_error());

// select record fields from database table and check for results

$sql=“SELECT * FROM horses WHERE breed=‘Abaco Barb’ AND gender=‘mare’ AND allowed=‘OK’ ORDER BY points DESC”;

$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);

if ($num_rows == 0) {
echo “Sorry, we have no records”;
} else {

echo "


Name
Horse ID
Player ID
Type
Points
Goal
Max Foals
Breeding Style
Fee
";

// table population

while ($row=mysql_fetch_array($mysql_result))
{
$name=$row[“name”];
$horse_id=$row[“horse_id”];
$player_id=$row[“player_id”];
$horse_type=$row[“horse_type”];
$points=$row[“points”];
$goal=$row[“goal”];
$max_foals=$row[“max_foals”];
$breeding_type=$row[“breeding_type”];
$fee=$row[“fee”];

// results & headings

echo "


$name
$horse_id
$player_id
$horse_type
$points
$goal
$max_foals
$breeding_type
$fee
";

}
}
mysql_close($connection);

?>[/code]

The error message I am receiving: http://www.virtualstudbook.co.uk/search … _barb1.php

Here is the code with the line numbers included:
http://www.virtualstudbook.co.uk/code_error.pdf

Many thanks.

The variable $connection is never defined. Try this:

[php]
$connection = mysql_connect("****", “", "”) or die(mysql_error());
[/php]

Sorry - we are nearly there this time!!

I have altered the code as per your comment, and I now receive a message stating there are no entries. However, I also receive an error message:

http://www.virtualstudbook.co.uk/search … _barb1.php

Here is the code with the line numbers included:
http://www.virtualstudbook.co.uk/code_error.pdf

Sorry to keep asking :oops:

There is no MySQL server running on the address given. Contact the host of that address and ask them what’s wrong.

Btw, your pdf file doesn’t show up.

I have other queries using same database/table working OK… as per my original example without the “no entries” message: http://www.virtualstudbook.co.uk/search … o_barb.php

PDF working fine here, perhaps a server blip last night?

Yes, it could indeed have been a server glitch, since the error message changed (for this reason, I’ll ask you to copy+paste the error onto the forums, we we’re not looking at two different problems).

The error message reads:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/fhlinux159/v/virtualstudbook.co.uk/user/htdocs/search/mare/abaco_barb1.php on line 39

The remedy is debugging (see the link in my signature). Use error_reporting(), and echo your query to see if it’s the query you’re expecting to run.

Sponsor our Newsletter | Privacy Policy | Terms of Service