从MySql&中填充下拉列表选项腓

时间:2017-02-23 03:32:26

标签: php mysql mysqli

希望这个问题不会让任何人喝酒。 我已经在这里待了大约9个小时,并且无法获得下降填充。我知道我错过了一些非常简单的事情,我正在伸出手去看看是否有人可以给我一些见解。我正在调用的表有1列,只有名称(这是唯一的)

我在所有页面的开头都调用了一个db类,除了2个案例之外,其他所有版本都工作正常,在2个例外中,它是需要下拉列表的页面。我已经删除并重新创建了两个页面而没有任何变化。

两个页面都命名为select.php和select_p.php。初始调用如下,并在第一个“ - >”处中断所以开始打印所有内容后直到“?>”

include("database.class.php");
$database = new database;
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
?>

继承了同一网站中其他2个网站和其他网页的类文件中的功能

function mysqlQuery($qry)

{   

    $rs     =   mysql_query($qry, $this->DatabaseLink);

    return $rs;



    echo mysql_error();

}

现在,如果我使用页面中的代码,它不会打印,但下拉列表是空白的

<select name='list' value=''><option>Select List</option>
<?
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>

我看过以下内容(加上大约20页并非如此接近)

PHP- Fetch from database and store in drop down menu html

How to put table value in a dropdown menu with MYSQL and PHP

http://www.plus2net.com/php_tutorial/list-table.php

http://www.tutorialrepublic.com/faq/how-to-populate-dropdown-list-with-array-values-in-php.php

以下是我尝试无效的所有代码片段,其中一些实际上是以html格式打印代码,任何建设性的帮助都会非常感激。

*********************************************************
<select name='list' value=''><option>Select List</option>
<?
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>
*****************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result = $database->mysqlQuery("select * from spec_tables");
if (!$result) die('Couldn\'t fetch records'); 
$num_fields = mysql_num_fields($result); 
$row = array(); 
for ($i = 0; $i < $num_fields; $i++) 
while ($row = mysql_fetch_row($result)) 
    {
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>
********************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result  = $database->mysqlQuery("select * from spec_tables");
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
   echo '<option value=". $row['name'] .">' . $row['name'] . '</option>';
}
}
?>
</select>
****************************************************************************
Placed in top of file
$servername = "localhost";
$username = "uname";
$password = "pword";
$dbname = "db_name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($result = mysqli->query("SELECT * FROM 'spec_tables'")) {
printf("Select returned %d rows.\n", $result->num_rows);

/* free result set */
$result->close();
}


**********************************************************************
 <?
$sql = "select * from spec_tables";
$result = mysql_query($sql);

echo "<select name='list' value=''><option>Select List</option>";

while ($row = mysql_fetch_array($result)) {

echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
?>
**************************************************

1 个答案:

答案 0 :(得分:0)

下载正在运行的代码

//placed in beginning of code
<?php
include("database.class.php");
$database = new Database;

$result  = $database->mysqlQuery("SELECT * FROM spec_tables");

?>
//Placed inside html form
 <?php
echo '<select name="list">';
echo '<option>Select List</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['name'] .'">' . $row['name'] .'</option>';
}
echo '</select>';

?>