Page 1 of 1

Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 2:33 pm
by colafran
I am new to PHP, bear with me :)

I am looking to populate a drop down list by doing a MySQL query using PHP. I found a code example, but it does not seem to work, can anyone tell me if there is something wrong with it? The posting I saw was from 2005, so I am wondering if the code is incompatible with PHP5?

Before you ask, I know that my database query is working, I cut and pasted it from another script that is working.

Another thing, the drop down does show 5 spaces, which it should be because the sql query is returning only 5 records, but the spaces are blank. I attached a screen cap of it.
ddown.JPG
ddown.JPG (3.08 KiB) Viewed 348 times

This is the exact code I have:


<?php
// Connect and select a database
mysql_connect("localhost", "root", "");
mysql_select_db("addressBook");

// Run a query
$result = mysql_query("SELECT * FROM colleague");
?>

<!- HTML form opening tags -->
<form action="action" method="post">
<select name="option">

<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[value]\">$row[title]</option>\n";
}
?>

<!- HTML form closing tags -->
</select>
<input type="submit">
</form>

Re: Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 2:52 pm
by kryles
$row[value] $row[title]

im not sure but wouldnt that be if you made your sql like

Code: Select all

 
 
$result=("SELECT value, title FROM tblname");
 
 
I usually list the fields so I'm not sure this way

Re: Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 2:59 pm
by flying_circus
Hey Colafran, looks like you are missing some single quotes. Try this:

Code: Select all

<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['value'] . '">' . $row['title'] . '</option>\n';
}
?>
The reason you are getting 5 blanks is because you need to wrap the array key in quotes. $row[value] is not the same as $row['value'];

Re: Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 3:01 pm
by flying_circus
kryles wrote:$row[value] $row[title]

im not sure but wouldnt that be if you made your sql like

Code: Select all

 
 
$result=("SELECT value, title FROM tblname");
 
 
I usually list the fields so I'm not sure this way
Explicitly defining the values you want in your SQL statement is not wrong, it just wont solve this problem. I generally do the same as you unless I need alot of fields from the database, then lazyness typically takes over and I use the wildcard *.

Re: Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 4:22 pm
by kryles
okay so behind the scenes PHP will recognize field names and when called $row['fieldName'] it'll know what you mean even when using asterix.....something new for me lol thanks :)

Re: Dynamically populating a drop down list via PHP

Posted: Thu Mar 13, 2008 4:35 pm
by flying_circus
kryles wrote:okay so behind the scenes PHP will recognize field names and when called $row['fieldName'] it'll know what you mean even when using asterix.....something new for me lol thanks :)
Exactly! No problem :)