ForEach PHP 5.4和5.3与MySQLi对象有不同的行为

时间:2013-11-19 16:38:27

标签: php mysqli

$mysqli = new mysqli("localhost","root","", "mydb") or die("Error " . mysqli_error($con)); 

$books = "select * from books";                                                         

$book_results = $mysqli->query($booked_pen_query);

$booked_arr = array();

foreach($book_results as $b){
    print_r($b);
    array_push($booked_arr, $b);
}

预期结果:Array ([book_id] => 1 [book_name] => "abc") Array ([book_id] => 2 [book_name] => "xyz")

它适用于PHP 5.4.19,但较低版本似乎显示为空白。

问题是:如何在PHP 5.3中执行此代码工作?

1 个答案:

答案 0 :(得分:0)

在PHP 5.4(http://us1.php.net/manual/en/class.mysqli-result.php

中更改了此行为
  

5.4.0添加了迭代器支持,因为mysqli_result现在实现了Traversable。

使其在PHP 5.3中运行:

while($b = $book_results->fetch_assoc()){
     print_r($b);
     array_push($booked_arr, $b);
}