Forum Moderators: coopster
Im trying to make this work but with little success.
Basically what im trying to do, is to echo a set of results if something is true in a particular field.
$data = mysql_query ("SELECT * FROM database WHERE animal='hamster' ORDER BY id DESC")
for example. But if there are no hamsters in the animal field, then i would like to display a message saying "no hamsters found" or something like that.
I've tried using isset, is_null, empty etc on the $data variable, but nothing seems to work. I either get nothing, or the 'no hamsters' or the actual data, but not when i want it.
If that makes any sense?
:)
but again for using that syntax your default needs to be set on NULL
another way:
use mysql_num_rows [php.net]
then do
if($num==o)
{
echo"aaaaaaaa";
}
else etc...
edit - a few minutes later - ok..no it hasn't worked.
for some reason, it wont display all of the data that belongs to that particular field.
i added this line
while ($info = mysql_fetch_array( $data ))
above and below this line
if ( mysql_num_rows( $data ) > 0 )
but it displayed every data, but also the "no hamsters line"
if ( mysql_num_rows( $data ) > 0 )
{
while ($info = mysql_fetch_array( $data )){
echo "Hamster# ".$info['id']. "; Hamster Name: ".$row['name']; //etc..
echo "<br />";
}
}
else {
echo 'There are no hamsters in the field! Feel free to let the cat out.';
}
//instead of $info['col_name'] you can use $info[0] (the array is also numbered..)
GL!
replace:
mysql_fetch_arraywith:
mysql_fetch_assocthen $info['id'] will contain a value.
Just a note for anyone who might need it..