Page 1 of 1

Populating Drop Down Menus with Select Statement

Posted: Wed Oct 29, 2003 3:30 am
by sergei
Can someone tell me how to populate a drop down menu using a select statement.

Thanks

Posted: Wed Oct 29, 2003 5:07 am
by twigletmac
You need to get all the records out using the SELECT statement, and then you just loop through the result putting them into <option> tags to make the select box.

Mac

Posted: Wed Oct 29, 2003 6:42 am
by sergei
I have this piece of code:

Code: Select all

<?

		$selectstatement = "SELECT Category_ID, Category_Type FROM category";
		$selectresult = mysql_query($selectstatement, $connection);			
		print "This works";
		while ($row = mysql_fetch_array($selectresult)) { 
			$Category_Type = $row['Category_Type'];			
			$id = $row['Category_ID'];
			if ($id == 1) {
				print("<option SELECTED value="$id">$Category_Type</option>\n"); 
			} else {
				print("<option value="$id">$Category_Type</option>\n"); 
			}
		} 
?>
When I test, it gives me the following errors:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Apache2\htdocs\metering\core.php on line 14
This works
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Apache2\htdocs\metering\core.php on line 16

Can anyone find the problem ? There is data in the database.

Posted: Wed Oct 29, 2003 6:44 am
by twigletmac
Where does $connection come from? Also it's a good idea to add error handling to mysql_query() calls:

Code: Select all

$selectresult = mysql_query($selectstatement, $connection) or die(mysql_error().'<p>'.$selectstatement.'</p>');
Mac

Posted: Wed Oct 29, 2003 7:04 am
by sergei

Code: Select all

<?

$connection = mysql_connect($DBHOST,$DBUSER) 
		or die("Connection unsuccessful: " . mysql_error()); 						
	mysql_select_db($DBASE) 
		or die("Connection to database unsuccessful: " . mysql_error()); 


?>
I've used the $connection variable successfully in the past. No changes have been made to it.

Posted: Wed Oct 29, 2003 8:38 am
by twigletmac
Did you add the or die() error handling to the mysql_query() call? If so did it do anything?

Mac