Wednesday, November 24, 2010

Let Learn PHP (Basic) - Lesson 4

Today, i want to show you how to do a drop down menu using database connection.



<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'my_db';
mysql_select_db($dbname);
$query = "SELECT CD_State, Desc_State FROM State";
$result = mysql_query($query);
?>
<select name="state">
<?php do { ?>
<option value="<?php echo $row['CD_State'];?>"><?php echo $row['Desc_State']; ?></option>
<?php } while($row = mysql_fetch_array($result, MYSQL_ASSOC))?>
</select>

OUTPUT 


 


In Programming, we must know to combine all kind of sources in order to get the exact figure that we want. In this post, i show you the drop down menu that i combine the code from Lesson 2 and 3...then we got Lesson 4...Isn't it wonderful...! Programming should be more interesting when we know how to do it...Good Luck!

2 comments:

LinkWithin

Related Posts Plugin for WordPress, Blogger...