我正在尝试在桌面上做一个简单的下拉列表,但是,我的代码似乎没有工作,我想知道我连接和检索的方式是否有任何问题?或者它只是我的下拉列表代码是错误的。以下是它的代码,下面的屏幕截图包含我的数据库以及我想放下我的下拉列表的地方。谢谢您的时间。
<?php
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$stmt = $mysqli->prepare("Select sbranch_name from branches");
$result = $stmt->execute();
$stmt->bind_result($sbranch_name);
//while ($stmt->fetch())
//{
// $stmt .="<option>". $row['sbranch_name']. "</option>";
//echo '<input type="checkbox" name="sbranch_name[]" value="'.$sbranch_name.'". <br>';
// echo $stmt;
//}
if ($result->num_rows > 0) {
echo "<select name='sbranch_name'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}
$stmt->close();
$mysqli->close();
?>
答案 0 :(得分:0)
试试这段代码
使用mysqli->query
<?php
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$sql="Select sbranch_name from branches";
$result = $mysqli->query($sql);
//$stmt->bind_result($sbranch_name);
//while ($stmt->fetch())
//{
// $stmt .="<option>". $row['sbranch_name']. "</option>";
//echo '<input type="checkbox" name="sbranch_name[]" value="'.$sbranch_name.'". <br>';
// echo $stmt;
//}
if ($result->num_rows > 0) {
echo "<select name='sbranch_name'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}
//$stmt->close();
$mysqli->close();
?>
答案 1 :(得分:0)
我认为绑定查询时的问题,为什么不使用下面的代码。
$result = $mysqli->query("Select sbranch_name from branches");
if ($result->num_rows > 0) {
echo "<select name='images'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}