从数据库中提取以形成选择

Working on a project where I am going to extract from my database and show the pool name in a form select. But very unsure how to go further than this.

$con=mysqli_connect("localhost","root","","nih_bw");
// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql = "Select name from pools";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');

 }

mysqli_close($con);

Try using your query like this:

<select name="batch">
    <option value="">Select One</option>
    <?php 
    $pd=$dbh->prepare("SELECT * FROM `pools`");
    $pd->execute();
    foreach($pd->fetchAll() as $rw) :
    ?>
        <option value="<?php echo $rw['id'];?>"><?php echo $rt['name'];?></option>
    <?php endforeach; ?>
</select>

It is in pdo, but you will get my logic.

You can do something like this:

<?php
$con=mysqli_connect("localhost","root","","nih_bw");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"Select name from pools");
echo "<select name='mypool'>";

$default_name = "foo_bar"; 
while($row = mysqli_fetch_array($result)){
  $opt_name = $row['name'];
  $str_selected = "";
  if($opt_name == $default_name){
      $str_selected = "selected";
  }
  echo "<option value='".$opt_name."' ".$str_selected." >" . $opt_name. "</option>";
}
echo "</select>";
mysqli_close($con);
?>

You can see an introductory article here:http://www.w3schools.com/Php/php_mysql_select.asp

Hope this helps.

I think this will work for you. (Code is not tested)

<?php
    $record = array();
    $con=mysqli_connect("localhost","root","","nih_bw");
    // Check connection
    if (mysqli_connect_errno($con)){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql = "Select name from pools";
    if(!$result = mysql_query($sql,$con)){
        die('There was an error running the query [' . $db->error . ']');
    }
    else{
        while($row = mysql_fetch_assoc($result)){
            $record[] = $row;
        }
    }

    mysqli_close($con);
?>
<select>
    <?php
        foreach($record as $value){
            echo "<option>".$value."</option>";
        }
    ?>
</select>