我刚刚在我的代码中遇到了一个错误,该错误在过去几个小时内无法修复。我正在从数据库中查询某些内容,然后尝试使用结果。但是,每当我尝试访问查询结果的内容时,脚本都会停止。
$recordcount = $db->query("SELECT optin_mail FROM games WHERE id = $game_id");
$result = $db->getrec();
echo "This will be executed without a problem";
if (isset ($result) && $result != null) echo $result;
echo "This won't be executed. The script stops in the line above";
我做了很多测试,发现每次我尝试分配$ result时,例如$newVariable = $result;
或尝试访问内容$content = $result["name"];
时,它都会停止。甚至回显它或使用print_r($result);
都会使脚本立即崩溃。
我真的不知道该怎么办了。我检查了查询,结果数为1。函数getrec()
基本上只是mysqli_fetch_assoc($queryResult);
,工作得很好,我在同一文件中多次使用它。
答案 0 :(得分:1)
好的,首先,如果不了解getrec()
方法的内在就很难说出问题所在。我的猜测是您正在尝试回显错误的数据类型。函数mysqli_fetch_assoc
返回一个数组,因此回显$result
无效。
尝试
$recordCount = $db->query(sprintf('SELECT optin_main FROM games WHERE id = %s', $game_id));
$results = $db->getrec();
if ($results ?? null) {
echo $results[0]['optin_main'];
}