Page 1 of 1

[SOLVED] Determining whether an array contains any values

Posted: Wed Oct 20, 2004 10:43 am
by chriso
I have a mysql table that I want to list its contents. However, if there is nothing in the table I want to print a message instead.

Currently I'm using this code:

Code: Select all

$crs_array = @mysql_query("SELECT * FROM schedchgs ORDER BY ADDDEL");
	if (!$crs_array) {
	die('<h2>Error retrieving schedule changes from database!</h2><br>' . 
	'Error: ' . mysql_error());
	}
echo ('<table border="0" align="center">');

while ($crs_row = mysql_fetch_array($crs_array)){
echo ('<tr><td width="60%">'.$crs_row[INFO].'</td><td>'.$crs_row[ADDDEL].'</td></tr>');
}
This will list the contents of the table. However, if the table is empty because it contains no data, I want to print a message instead. I've tried various if else statements but have had no success because I'm not sure how to evaluate the array.

Thanks for your help.

Posted: Wed Oct 20, 2004 10:46 am
by feyd
[php_man]mysql_num_rows()[/php_man]

and quote your string indices into arrays.

Posted: Wed Oct 20, 2004 10:49 am
by Wayne

Code: Select all

if ( mysql_num_rows($crs_array) > 0 ) {
    while ( $crs_row = mysql_fetch_array($crs_array) ) {
        echo ('<tr><td width="60%">'.$crs_row[INFO].'</td><td>'.$crs_row[ADDDEL].'</td></tr>');
    }
}
else {
    echo "no rows here!";
}

Posted: Wed Oct 20, 2004 10:54 am
by chriso
feyd wrote:[php_man]mysql_num_rows()[/php_man]

and quote your string indices into arrays.
Thanks for the quick reply :D . Works great.