19

I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)

Here's my Query: Select PcID from PC;

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
Donnie
  • 243
  • 1
  • 3
  • 11
  • do you know how to code a drop-down box by hand? that would be a good start. also, your whole query didn't make it to the page. – jcomeau_ictx Mar 04 '11 at 04:33
  • are you saying that you need to create the html for a select box and populate the options with data returned from a query? – Kyle Mar 04 '11 at 04:34
  • @ Cybermate - I am new to PHP @jcomeau_ictx: Yes I know how to code a drop down box by hand, @ Kyle: I need the PHP code, and from the other comments I am assuming I forgot to code the skeleton of the Drop down box. – Donnie Mar 04 '11 at 04:40

7 Answers7

31

You will need to make sure that if you're using a test environment like WAMP set your username as root. Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.

<?php

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);

echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";

?>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • Okay, I followed your code, and I moved it over to the server, and now the page doesn't load, am i forgetting some HTML code? – Donnie Mar 04 '11 at 04:37
  • i thinks echo ""; – sush Mar 04 '11 at 04:42
  • A server error, as in the web page won't even load with that code – Donnie Mar 04 '11 at 04:42
  • @wlashell HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. – Donnie Mar 04 '11 at 04:47
  • Problem solved :) I messed up the code some how in the copy paste process, Deleted it, and just typed it all back in... – Donnie Mar 04 '11 at 05:01
  • 1
    is there any possible way to modify this for radio buttons? – roro May 03 '14 at 15:12
  • @roro Just echo the HTML for radio buttons instead of options in the loop, and remove the select tags. It's the same logic. – Dan Grossman Jun 23 '14 at 20:08
5

Below is the code for drop down using MySql and PHP:

<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">"; 
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q)) 
{        
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>"; 
}
echo "</select>";
?>
Harmlezz
  • 7,972
  • 27
  • 35
sush
  • 5,897
  • 5
  • 30
  • 39
4

Since mysql_connect has been deprecated, connect and query instead with mysqli:

$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);

And then, if you have more than one option list with the same values on the same page, put the values in an array:

while ($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

And then you can loop the array multiple times on the same page:

foreach ($rows as $row) {
    print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}
motorbaby
  • 634
  • 7
  • 18
0

No need to do this:

while ($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

You can directly do this:

while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
    }
0

After a while of research and disappointments....I was able to make this up

     <?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>

//insert the below code in the body


    <table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
    <th style="width:20%;">Email</th>
       <th style="width:10%;">City/ Region</th>
        <th style="width:30%;">Details</th>
  </tr>
  <?php
   while ($row = mysqli_fetch_array($result)) {

               echo "<tr>";
               echo "<td>".$row['username']."</td>";
               echo "<td>".$row['city']."</td>";
                echo "<td>".$row['details']."</td>";
               echo "</tr>";
           }

     ?>
</table>

Trust me it works :)

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
0

At the top first set up database connection as follow:

<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?> 

Then include the following code in HTML inside form

<select name="selected_pcid" id='selected_pcid'>

            <?php 

             while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
                        $value= $rows['id'];
                ?>
                 <option value="<?= $value?>"><?= $value?></option>
                <?php } ?>
             </select>

However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.

Bhupesh Shrestha
  • 248
  • 3
  • 17
0

What if you want to use both id and name in the dropdown? Here is the code for that:

$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);

echo "<select id='brandId' name='brandName'>";

while ($row = mysqli_fetch_array($result)) {
   unset($id, $name);
   $id = $row['BrandID'];
   $name = $row['BrandName']; 
   echo '<option value="'.$id.'">'.$name.'</option>';
 }
 echo "</select>";
rjose
  • 557
  • 5
  • 13