1

我正在尝试将我的代码从 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(); 

上面的代码试图在下拉菜单中显示与它们所属的建筑物匹配的所有房间。

谢谢

4

1 回答 1

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;  
} 
于 2012-10-25T16:06:26.970 回答