我正在尝试使用PHP从MySQL数据库填充下拉列表。现在我的下拉列表中没有任何内容。我已确保拥有正确的数据库名称,服务器名称,用户名和密码。
这是我的PHP文件queryfunction.php:
<?php
function connect(){
$servername = "localhost";
$username = "root";
$password = "******";
$database = "lab4";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
mysqli_select_db($database);
}
function close(){
mysqli_close();
}
function query(){
$mydata = mysqli_query("Select * from country");
while($record = mysqli_fetch_array($mydata)){
echo '<option value = "' . $record['CountryAbbreviation'] . '">' .$record['CountryAbbreviation'] . '"</option>';
}
}
?>
这是我的PHP文件,我称之为:
<?php
include_once "queryfunction.php";
connect();
?>
<!DOCTYPE html/>
<html>
在表格中的文档中进一步向下:
<tr>
<th>Country</th>
<td>
<select name="CountryDropDown">
<?php query() ?>
</select>
<?php close()?>
</td>
</tr>
答案 0 :(得分:0)
尝试将此行while($record = mysqli_fetch_array($mydata)){
更改为此while($record = mysqli_fetch_array($mydata,MYSQLI_ASSOC)){
或向我们展示您的$record
变量数据