它只返回false而不是有效的对象。PHP mysqli get_result()在语句上使用get_result()时返回false
function getGames() {
$ret = array();
$stmt = DB::getConnection()->prepare("SELECT * FROM games ORDER BY position DESC");
$stmt->execute();
var_dump($stmt);
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "read one entry";
}
return $ret;
}
所列出的var_dump返回此消息,我不能提取信息的任何形式:
object(mysqli_stmt)#3 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(5) ["errno"]=> int(2006) ["error"]=> string(26) "MySQL server has gone away" ["error_list"]=> array(1) { [0]=> array(3) { ["errno"]=> int(2006) ["sqlstate"]=> string(5) "HY000" ["error"]=> string(26) "MySQL server has gone away" } } ["sqlstate"]=> string(5) "HY000" ["id"]=> int(1) }
我的DB类看起来是这样的:
<?php
class DB {
private static $connection;
static function getConnection() {
if (!isset($connection) || $connection->ping()) {
$connection = new mysqli("localhost", "user", "pw", "db");
$connection->query("SET NAMES 'utf8'");
}
return $connection;
}
}
任何帮助,得到这个工作将不胜感激
代替'echo“读取一个条目”;'编写'$ ret [] = $ row;'然后调用函数并将其赋值给一个变量,然后打印该变量并检查将要发生什么? –
我已经得到'致命错误:调用一个成员函数fetch_assoc()在第15行的file.php中的boolean中,这是while循环所在的行 – Sabbertran
在静态方法中,使用'self :: $连接'来访问静态属性。你的“getConnection()”方法会在每次调用时创建新的连接 –