fetch_assoc时出现致命错误

时间:2015-09-14 17:05:49

标签: php web mysqli

我收到此错误:

  

致命错误:在第13行的C:\ xampp \ htdocs \ cronjob \ SQLreader.php中调用boolean上的成员函数fetch_assoc()

<?php
//readout Top10
// Hier mySQL - Zugangsdaten eingeben.
$servername = "*";
$username = "*";
$password = "*";
$db = "*";

$conn = new mysqli($servername, $username, $password, $db);

$user = $conn->query("SELECT * FROM btcstats ORDER BY kdr DESC LIMIT 10");

while($a = $user->fetch_assoc()) {
    $file = fopen("statscache.txt", "w+");
    fwrite($file, $a['uuid'] . "|" . $a['kills'] . "|" . $a['deaths'] . "|" . $a['gamesplayed'] . $a['gameswon'] . "|/n");
    fclose($file);
}

所以我们在这里遇到了问题,有些人可以找到错误吗?

1 个答案:

答案 0 :(得分:-1)

答案在mysqli::query documentation

  

失败时返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。

显然发生了MySQL错误,函数返回'false'。

$user = $conn->query("SELECT * FROM btcstats ORDER BY kdr DESC LIMIT 10");

if ($user) {
    while($a = $user->fetch_assoc()) {
        $file = fopen("statscache.txt", "w+");
        fwrite($file, $a['uuid'] . "|" . $a['kills'] . "|" . $a['deaths'] . "|" . $a['gamesplayed'] . $a['gameswon'] . "|/n");
        fclose($file);
    }
} else {
    die ("Mysql error: " . $conn->error);
}