Closed Thread Icon

Topic awaiting preservation: Determine if a WHILE function is empty (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12276" title="Pages that link to Topic awaiting preservation: Determine if a WHILE function is empty (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Determine if a WHILE function is empty <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-20-2002 16:33

I have a MySQL fetch line like this:

while ($row = mysql_fetch_array($result)) {
do stuff here
}


But I need to determine if the there are any results so I can print a "no results available" type statement. If there is data, the line works fine. But if there isn't I get nothing (which is fine). But I need to determine if it's nothing.

I tried using $num_classes = mysql_num_rows($row); and then checking if $num_classes was > 0, but that didn't work.

Any help would be greatly appreciated.

Thanks!

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 06-20-2002 16:43

I have no idea if there is a specific function/property that tells you if there are any results, but couldn't you do something like:

$results = 0;

while ($row = mysql_fetch_array($result)) {
do stuff here
$results = 1;
}

if (!$results)
echo("No results available!");

edit: forgot that variables' names start with a $ in PHP...

kuckus (cell #282)

[This message has been edited by kuckus (edited 06-21-2002).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-20-2002 16:52

If there's a way to get the length of the array returned by mysql_fetch_array($result), then you can compare that and see if it's greater than zero.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-20-2002 22:37

mysql_num_rows()

code:
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
do stuff here
}
}
else
{
echo("Null result set.");
}



mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 06-21-2002 06:39

Pugzly, in case you don't notice at first from WarMage's code from above, your call for mysql_num_rows() function was wrong, you passed $row variable (which is probably blank) instead of $results variable, which points to results returned from SQL query...


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 06-22-2002 02:00

You could also use

code:
if (!$result = mysql_query($sql))
{
echo "no results returned";
}
else
{
while($row = mysql_fetch_array($result))
{
do stuff here
}
}



-Butcher-


[This message has been edited by butcher (edited 06-22-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu