我想从MySQL数据库中用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>
再往下在doc:
<tr>
<th>Country</th>
<td>
<select name="CountryDropDown">
<?php query() ?>
</select>
<?php close()?>
</td>
</tr>
您至少有2个问题 - (1)变量作用域问题,请参阅http://php.net/manual/en/language.variables.scope.php as'$ conn '在'connect()'中定义,但在该函数外部不可用。 (2)你有'$ mydata = mysqli_query(“Select * from country”);'但是'mysqli_query()'需要连接作为第一个参数 - $ mydata = mysqli_query($ conn,“Select * from country”) ;',但第一个问题需要解决才能使用。 – Sean