Jump to content

Drop down list population


s4salman

Recommended Posts

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

?>
Link to comment
Share on other sites

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

?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.