0

I am trying to put rows from my database to a select
this is the code,

<select name='network' id='network'>
        <option selected>None</option>
         <?php
          $query = mysql_query("SELECT * FROM `countries` ");
       while($row=mysql_fetch_array($query))
        {
            echo $row['name'].'<br>';
        }
        ?>
        </select>
3
Contributors
5
Replies
14
Views
3 Years
Discussion Span
Last Post by SimonIoa
0

you mean if i have a connection with the db?
Yes because when i try

 <?php
          $query = mysql_query("SELECT * FROM `countries` ");
       while($row=mysql_fetch_array($query))
        {
            echo $row['name'].'<br>';
        }
        ?>

the data are return correctly

0

OK, so you don't have a problem then? Is there anything going wrong or did you just post a code snippet?
Oh wait, I see. Your <option> isn't displaying correctly is it?

you need to echo the actual option tag as well or you're just printing rows to the page as text.

echo '<option>'. $row['name'] . '</option>'

0

Are you pulling all the countries from your DB? or are their just select contries in your DB

typically I have an array of states or countries before I call this.

$countries = array("list of all the countries");
$country = "USA";

echo '<select name="country">';
foreach($countries as $key => $value){
    echo '<option value="'.$key.'"';
    echo ($key == $country)?' selected="selected"':'';
    echo '>'.$value.'</option>';
}
echo '</select>

Also if you can see I have used a short hand if statement. It is super useful and saves some lines. If you want I can explain it a little more.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.