我正在尝试将我的代码从mysql更改为mysqli,但我无法更改这段代码:
while($sqlrow = mysql_fetch_array($sqlresult))
{
$room = $sqlrow['Room'];
$roomHTML .= "<option value='".$room."'>" . $room . "</option>".PHP_EOL;
}
echo $roomHTML;
我在下面的mysqli代码中没有变量$ sqlrow,我如何将行$room = $sqlrow['Room'];
更改为:
完整代码:
$building = isset($_POST['building']) ? $_POST['building'] : '';
$sql = "SELECT Room FROM Room WHERE Building = ?";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("s",$building);
$sqlstmt->execute();
$sqlstmt->bind_result($dbRoom);
$roomHTML = "";
while($sqlstmt->fetch()) {
$room = $sqlrow['Room'];
$roomHTML .= "<option value='".$room."'>" . $room . "</option>".PHP_EOL;
}
echo $roomHTML;
$sqlstmt->execute();
上面的代码试图在下拉菜单中显示与其所属建筑相匹配的所有房间。
由于
答案 0 :(得分:0)
您没有使用绑定结果var $dbRoom
。
// Your output value of interest is $dbRoom as you have bound here:
$sqlstmt->bind_result($dbRoom);
$roomHTML = "";
while($sqlstmt->fetch()) {
// $dbRoom holds the value fetched from the result set, since that is what you bound above.
// Note: added htmlentities() to encode quotes if there are any embedded in the room value.
$roomHTML .= "<option value='". htmlentities($dbRoom, ENT_QUOTES) ."'>" . htmlspecialchars($dbRoom) . "</option>".PHP_EOL;
}