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...
Here's the getSetting
function:
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);
}
}
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');
}
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:
You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.
prepare
return a boolean only when it fails thereforeFALSE
, to avoid the error you need to check if it isTrue
first before executing:The problem lies in:
The
prepare()
method can returnfalse
and you should check for that. As for why it returnsfalse
, perhaps the table name or column names (inSELECT
orWHERE
clause) are not correct?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.)Another situation that can cause this problem is incorrect casting in your queries.
I know it may sound obvious, but I have run into this by using
tablename
instead ofTablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.