Forum Moderators: coopster

Message Too Old, No Replies

mysql query - no results, then echo something

         

surrealillusions

6:38 pm on May 14, 2008 (gmt 0)

10+ Year Member



Hi all,

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?

:)

WesleyC

7:27 pm on May 14, 2008 (gmt 0)

10+ Year Member



Try something like this...


$data = mysql_query ("SELECT * FROM database WHERE animal='hamster' ORDER BY id DESC");

if ( mysql_num_rows( $data ) > 0 )
{
// Process hamsters
}
else
{
echo 'There are no hamsters in the field! Feel free to let the cat out.';
}

henry0

7:28 pm on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you have a field (animal) with a default of NULL
then you may use:
WHERE
animal
IS NOT NULL

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

surrealillusions

8:20 pm on May 14, 2008 (gmt 0)

10+ Year Member



WesleyC - thanks! it worked how i needed it to be.

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"

ag_47

4:04 am on May 15, 2008 (gmt 0)

10+ Year Member



>>
$data = mysql_query ("SELECT * FROM database WHERE animal='hamster' ORDER BY id DESC");

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!

fabricator

4:20 am on May 15, 2008 (gmt 0)

10+ Year Member



replace:
mysql_fetch_array

with:
mysql_fetch_assoc

then $info['id'] will contain a value.

surrealillusions

10:38 am on May 15, 2008 (gmt 0)

10+ Year Member



thanks!

its working now

:)

(well..untill i screw it up again somehow ;) )

ag_47

4:33 pm on May 15, 2008 (gmt 0)

10+ Year Member



replace:
mysql_fetch_array

with:
mysql_fetch_assoc

then $info['id'] will contain a value.


You can specify which type of array to return with mysql_fetch_array.
The options are: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.
ex: $res = mysql_fetch_array($res, MYSQL_NUM);

Just a note for anyone who might need it..