s4salman Posted March 19, 2016 Share Posted March 19, 2016 Anyone could help me out? Drop down list is not populating from mysql table, i could not figured out what is wrong in it. <?php $dbhost = 'localhost'; $dbuser = 's4salman_jag'; $dbpass = 'jag001'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT * FROM tutorial'; mysql_select_db('s4salman_jag'); $retval = mysql_query( $sql, $conn ); echo "<select name=stepa>"; // list box select command foreach ($dbo->query($sql) as $row){//Array or records stored in $row echo "<option value=$row[field1]>$row[field1]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> Quote Link to comment https://forums.phpfreaks.com/topic/301043-drop-down-list-population/ Share on other sites More sharing options...
s4salman Posted March 19, 2016 Author Share Posted March 19, 2016 Got it to work as: <?php $host_name = "localhost"; $database = "s4salman_jag"; // Change your database name $username = "s4salman_jag"; // Your database user id $password = "jag001"; // Your password //////// Do not Edit below ///////// try { $dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } $sql="SELECT * FROM tutorial"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ echo "<select name=stepa>"; // list box select command foreach ($dbo->query($sql) as $row){//Array or records stored in $row echo "<option value=$row[field1]>$row[field1]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> Quote Link to comment https://forums.phpfreaks.com/topic/301043-drop-down-list-population/#findComment-1532209 Share on other sites More sharing options...
ginerjm Posted March 19, 2016 Share Posted March 19, 2016 Really? Your option tags don't look like they should have anything in them. echo "<option value=$row[field1]>$row[field1]</option>"; I think they s/b like this: echo "<option value=\"$row['fields']\">$row['fields']</option>"; The value attribute s/b quoted as should the indices of your array reference. Quote Link to comment https://forums.phpfreaks.com/topic/301043-drop-down-list-population/#findComment-1532217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.