致命错误:在布尔值上调用成员函数bind_param() [英] Fatal error: Call to a member function bind_param() on boolean

查看:96
本文介绍了致命错误:在布尔值上调用成员函数bind_param()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着从数据库获取设置的功能,突然,我遇到了这个错误:

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,也许表名或列名(在SELECTWHERE子句中)不正确?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆