将pg_query的结果放入选择下拉框

时间:2018-07-17 12:54:56

标签: php html postgresql psql

我正在运行一个pg_query,它在我创建的用户表的uid列中显示所有数据。 我需要的是将结果显示在我创建的用户名选择选择下拉框中,而不是在html页面上。我的代码如下:

<?php 
    include_once 'newheader.php' ;
 ?>
 <!-- css for the page-->
 <section class="main-container">
    <div class="main-wrapper">
        <h2 class="page_title">New User</h2>
    </div>
<!-- dropdown list for all user names-->    
     <p>
What is your User Name?
<select name="Usernameselect">
  <option value="">Select...</option>
  <option value="uid">'$row[0]'</option>  
</select>
</p>
  </section>  
<?php
// connect to database
$conn = pg_pconnect("host=localhost dbname=vcbv2 user=postgres");
if (!$conn) {
  echo "An error occurred.\n";
  exit;
}
// get all the uid from the uid column in users
$result = pg_query($conn, "SELECT uid FROM users");
if (!$result) {
  // error message  
  echo "An error occurred.\n";
  exit;
}
// dispaly on screen all uid data from users
while ($row = pg_fetch_row($result)) {
  echo "$row[0]";
  echo "<br />\n";
}
?>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

<?php 
    include_once 'newheader.php' ;
 ?>
 <!-- css for the page-->
 <section class="main-container">
    <div class="main-wrapper">
        <h2 class="page_title">New User</h2>
    </div>
<!-- dropdown list for all user names-->    
     <p>
What is your User Name?
<select name="Usernameselect">
  <option value="">Select...</option>
<?php
// connect to database
$conn = pg_pconnect("host=localhost dbname=vcbv2 user=postgres");
if (!$conn) {
  echo "An error occurred.\n";
  exit;
}
// get all the uid from the uid column in users
$result = pg_query($conn, "SELECT uid,name FROM users");
if (!$result) {
  // error message  
  echo "An error occurred.\n";
  exit;
}
// dispaly on screen all uid data from users
while ($row = pg_fetch_row($result)) {
  echo '<option value="'.$row[0].'">'.$row[1].'</option>';
}
?>
</select>
</p>
</section>