函数内 fetch_assoc 的致命错误 [英] Fatal Error with fetch_assoc inside a function
问题描述
我真的需要你的帮助.我已经看到了答案,甚至遵循了之前发布的建议,但没有解决方案显示如何使用 PREPARED STATEMENTS
获取关联数组,所以我不想提交已经回答的问题.我已遵循其他问题的建议,但我仍然收到:
I truly need your help. I have seen answers and even followed advice as previously posted but NO SOLUTIONS SHOW how to use fetch associated array using PREPARED STATEMENTS
so I am not trying to submit an already answered question. I have followed suggestions to others questions but I am still getting:
致命错误:在第 75 行/xxx/xxxxxx.php 中的非对象上调用成员函数 fetch_assoc()
Fatal error: Call to a member function fetch_assoc() on a non-object in /xxx/xxxxxx.php on line 75
我正在向您展示使用 MySQL 的旧代码以及我如何使用准备好的语句将其更改为 MySQLi,但我仍然遇到相同的致命错误.有人可以帮助我吗,我快疯了,我真的需要找出我做错了什么.感谢您的帮助.
I am showing you the old code using MySQL and how I have changed it to MySQLi using prepared statements, but I am still getting the same Fatal Error. Can someone please help me, I am going crazy and I truly need to find out what I am doing wrong. Thanks for your help.
//*******************************************************************
// OLD CODE:
// Called by: $theme=$log->get_theme();
//*******************************************************************
class redirect
{
function __construct()
{
}
function get_theme()
{
$rs=mysql_query("select * from theme where status='yes'");
if(mysql_num_rows($rs)>0)
{
$data=mysql_fetch_array($rs);
return $data['theme_name'];
}
}
}
//*********************************************************************
// OLD CODE:
// Called by: $theme=$log->get_theme($cn);
//*********************************************************************
class redirect
{
public $cn;
function __construct()
{
}
function get_theme($cn)
{
$themeStatus = 'yes';
if ($stmt = $cn->prepare("SELECT * FROM theme WHERE status = ? "))
{
$stmt->bind_param("s", $themeStatus);
$result = $stmt->execute();
$stmt->store_result();
if ($stmt->num_rows >= "1")
{
$data = $result->fetch_assoc(); // ERROR LINE
return $data['theme_name'];
}
}
else
{
echo "Failed to execute prepared statement: " . mysqli_connect_error();
}
}
}
推荐答案
mysqli_stmt::execute 方法根据定义只返回 bool.所以调用 $result->any_method_name()
将会失败,因为 $result
是一个布尔值.
The mysqli_stmt::execute method returns only bool by definition. So calling $result->any_method_name()
will fail because $result
is a boolean value.
要使用 MySQLi 库从准备好的语句中获取值,请使用 $stmt->bind_result(...)
绑定目标变量,然后使用 $stmt->fetch()
在 while 循环中获取绑定变量中的查询结果.之后你从 MySQLi 切换到 PDO 因为它有一个更好的 API ......
To get the values from a prepared statement using the MySQLi library you bind your target variables with $stmt->bind_result(...)
and then use $stmt->fetch()
in a while loop to get the result of your query in your bound variables. And after that you switch from MySQLi to PDO because it has a better API regarding this…
这篇关于函数内 fetch_assoc 的致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!