致命错误:在布尔值上调用成员函数 bind_param() [英] Fatal error: Call to a member function bind_param() on boolean
问题描述
我正忙于从数据库获取设置的函数,突然,我遇到了这个错误:
I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error:
Fatal error: Call to a member function bind_param() on boolean in C:\xampp2\htdocs\application\classes\class.functions.php on line 16
通常,这意味着我要从不存在的表格和内容中选择内容.但在这种情况下,我不是......
Normally, this would mean that I'm selecting stuff from unexisting tables and stuff. But in this case, I 'm not...
这是 getSetting
函数:
public function getSetting($setting)
{
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
$query->execute();
$query->bind_result($value, $param);
$query->store_result();
if ($query->num_rows() > 0)
{
while ($query->fetch())
{
return $value;
if ($param === '1')
{
$this->tpl->createParameter($setting, $value);
}
}
}
else
{
__('invalid.setting.request', $setting);
}
}
$this->db
变量通过构造函数传递.如有需要,这里是:
The $this->db
variable is passed through a constructor. In case of need, here is it:
public function __construct($db, $data, $tpl)
{
$this->db = $db;
$this->tpl = $tpl;
$this->data = $data;
$this->data->setData('global', 'theme', $this->getSetting('theme'));
}
此外,由于我正在使用数据库,因此我的数据库连接:
Also, since I'm making use of a database, my database connection:
class Database
{
private $data;
public function __construct($data)
{
$this->data = $data;
$this->conn = new MySQLi(
$this->data->getData('database', 'hostname'),
$this->data->getData('database', 'username'),
$this->data->getData('database', 'password'),
$this->data->getData('database', 'database')
);
if ($this->conn->errno)
{
__('failed.db.connection', $this->conn->errno);
}
date_default_timezone_set('Europe/Amsterdam');
}
我已经测试了连接,100% 肯定它按预期工作.我在配置文件中设置数据库连接:
I've already tested the connection, 100% positive that it works as intended. I'm setting the DB connection things in a configuration file:
'database' => array(
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => ******,
'database' => 'wscript'
)
现在奇怪的是;表存在,请求的设置存在,数据库存在,但仍然不会留下该错误.下面是一些证明数据库正确的证据:
Now the weird thing is; the table exists, the requested setting exists, the DB exists, but still, that error won't leave. Here's some proof that the DB is correct:
推荐答案
问题出在:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
prepare()
方法可以返回 false
并且您应该检查它.至于为什么返回false
,可能是表名或列名(在SELECT
或WHERE
子句中)不正确?
The prepare()
method can return false
and you should check for that. As for why it returns false
, perhaps the table name or column names (in SELECT
or WHERE
clause) are not correct?
另外,考虑使用类似 $this->db->conn->error_list
检查在解析 SQL 时发生的错误.(我偶尔也会回显实际的 SQL 语句字符串并粘贴到 phpMyAdmin 中进行测试,但肯定会出现问题.)
Also, consider use of something like $this->db->conn->error_list
to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)
这篇关于致命错误:在布尔值上调用成员函数 bind_param()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!