[SOLVED] Determining whether an array contains any values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

[SOLVED] Determining whether an array contains any values

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]mysql_num_rows()[/php_man]

and quote your string indices into arrays.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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!";
}
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

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