Fatal error: Call to a member function bind_param() on boolean
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
up vote
48
down vote
favorite
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:xampp2htdocsapplicationclassesclass.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:
php mysql
add a comment |
up vote
48
down vote
favorite
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:xampp2htdocsapplicationclassesclass.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:
php mysql
add a comment |
up vote
48
down vote
favorite
up vote
48
down vote
favorite
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:xampp2htdocsapplicationclassesclass.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:
php mysql
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:xampp2htdocsapplicationclassesclass.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:
php mysql
php mysql
edited Aug 23 '17 at 22:29
![](https://i.stack.imgur.com/XgeJ5.png?s=32&g=1)
![](https://i.stack.imgur.com/XgeJ5.png?s=32&g=1)
ROMANIA_engineer
32.2k19141140
32.2k19141140
asked Dec 10 '14 at 6:39
![](https://i.stack.imgur.com/Cqj69.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Cqj69.jpg?s=32&g=1)
Wesley Peeters
90421838
90421838
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
up vote
97
down vote
accepted
The problem lies in:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
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?
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.)
2
Why do you suggest to call$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?
– Adam
May 11 '16 at 13:01
1
@Adam you have a point. Calling$query->error_list
makes sense if the bind_param or an execute fails, but if$query
is false then one should callerror
orerror_list
on the mysqli object, i.e.$this->db->conn->error_list
.
– RobP
Dec 13 '16 at 14:53
add a comment |
up vote
17
down vote
Any time you get the...
"Fatal error: Call to a member function bind_param() on boolean"
...it is likely because there is an issue with your query. The prepare()
might return FALSE
(a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!
First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log
. If you're examining error logs in a Linux environment you can use tail -f /path/to/log
in a console window to see errors as they occur in real-time....or as you make them.
Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
The error is generic and not very helpful to you in solving what is going on.
With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare()
statement for truthiness and if it is good you can proceed on to binding and executing.
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
// any additional code you need would go here.
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
}
If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo
column in the table, solving the problem is trivial.
If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
add a comment |
up vote
11
down vote
prepare
return a boolean only when it fails therefore FALSE
, to avoid the error you need to check if it is True
first before executing:
$sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
if($query = $this->db->conn->prepare($sql)){
$query->bind_param('s', $setting);
$query->execute();
//rest of code here
}else{
//error !! don't go further
var_dump($this->db->error);
}
add a comment |
up vote
6
down vote
Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
Always close your previous statement with
$statement->close();
If the syntax is correct, the following query will run well too.
This answer helped me.
– Amine
Jan 4 at 17:23
add a comment |
up vote
1
down vote
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 of Tablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.
add a comment |
up vote
0
down vote
Sometimes, it is also because of a wrong table name or column name in the prepare statement.
See this:
https://www.youtube.com/watch?v=LGHdQ1bBho8
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
This particular error has very little to do with the actual error. Here is my similar experience and the solution...
I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...
add a comment |
up vote
0
down vote
Following two are the most probable cause of this issue:
- Spelling mistake either in column names or table name
- Previously established statement is not closed. Just close it before making the prepared statement.
$stmt->close(); // <<<-----This fixed the issue for me
$stmt = $conn->prepare("Insert statement");
add a comment |
up vote
0
down vote
Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?)
may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)
add a comment |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
97
down vote
accepted
The problem lies in:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
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?
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.)
2
Why do you suggest to call$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?
– Adam
May 11 '16 at 13:01
1
@Adam you have a point. Calling$query->error_list
makes sense if the bind_param or an execute fails, but if$query
is false then one should callerror
orerror_list
on the mysqli object, i.e.$this->db->conn->error_list
.
– RobP
Dec 13 '16 at 14:53
add a comment |
up vote
97
down vote
accepted
The problem lies in:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
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?
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.)
2
Why do you suggest to call$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?
– Adam
May 11 '16 at 13:01
1
@Adam you have a point. Calling$query->error_list
makes sense if the bind_param or an execute fails, but if$query
is false then one should callerror
orerror_list
on the mysqli object, i.e.$this->db->conn->error_list
.
– RobP
Dec 13 '16 at 14:53
add a comment |
up vote
97
down vote
accepted
up vote
97
down vote
accepted
The problem lies in:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
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?
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.)
The problem lies in:
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
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?
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.)
edited Apr 1 '17 at 2:20
miken32
22.8k84671
22.8k84671
answered Dec 10 '14 at 6:43
![](https://i.stack.imgur.com/sYZ8F.png?s=32&g=1)
![](https://i.stack.imgur.com/sYZ8F.png?s=32&g=1)
RobP
5,97421532
5,97421532
2
Why do you suggest to call$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?
– Adam
May 11 '16 at 13:01
1
@Adam you have a point. Calling$query->error_list
makes sense if the bind_param or an execute fails, but if$query
is false then one should callerror
orerror_list
on the mysqli object, i.e.$this->db->conn->error_list
.
– RobP
Dec 13 '16 at 14:53
add a comment |
2
Why do you suggest to call$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?
– Adam
May 11 '16 at 13:01
1
@Adam you have a point. Calling$query->error_list
makes sense if the bind_param or an execute fails, but if$query
is false then one should callerror
orerror_list
on the mysqli object, i.e.$this->db->conn->error_list
.
– RobP
Dec 13 '16 at 14:53
2
2
Why do you suggest to call
$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?– Adam
May 11 '16 at 13:01
Why do you suggest to call
$query->error_list
if the prepare methods returns false (and thus $query would be false?) when an error appears?– Adam
May 11 '16 at 13:01
1
1
@Adam you have a point. Calling
$query->error_list
makes sense if the bind_param or an execute fails, but if $query
is false then one should call error
or error_list
on the mysqli object, i.e. $this->db->conn->error_list
.– RobP
Dec 13 '16 at 14:53
@Adam you have a point. Calling
$query->error_list
makes sense if the bind_param or an execute fails, but if $query
is false then one should call error
or error_list
on the mysqli object, i.e. $this->db->conn->error_list
.– RobP
Dec 13 '16 at 14:53
add a comment |
up vote
17
down vote
Any time you get the...
"Fatal error: Call to a member function bind_param() on boolean"
...it is likely because there is an issue with your query. The prepare()
might return FALSE
(a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!
First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log
. If you're examining error logs in a Linux environment you can use tail -f /path/to/log
in a console window to see errors as they occur in real-time....or as you make them.
Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
The error is generic and not very helpful to you in solving what is going on.
With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare()
statement for truthiness and if it is good you can proceed on to binding and executing.
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
// any additional code you need would go here.
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
}
If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo
column in the table, solving the problem is trivial.
If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
add a comment |
up vote
17
down vote
Any time you get the...
"Fatal error: Call to a member function bind_param() on boolean"
...it is likely because there is an issue with your query. The prepare()
might return FALSE
(a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!
First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log
. If you're examining error logs in a Linux environment you can use tail -f /path/to/log
in a console window to see errors as they occur in real-time....or as you make them.
Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
The error is generic and not very helpful to you in solving what is going on.
With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare()
statement for truthiness and if it is good you can proceed on to binding and executing.
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
// any additional code you need would go here.
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
}
If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo
column in the table, solving the problem is trivial.
If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
add a comment |
up vote
17
down vote
up vote
17
down vote
Any time you get the...
"Fatal error: Call to a member function bind_param() on boolean"
...it is likely because there is an issue with your query. The prepare()
might return FALSE
(a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!
First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log
. If you're examining error logs in a Linux environment you can use tail -f /path/to/log
in a console window to see errors as they occur in real-time....or as you make them.
Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
The error is generic and not very helpful to you in solving what is going on.
With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare()
statement for truthiness and if it is good you can proceed on to binding and executing.
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
// any additional code you need would go here.
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
}
If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo
column in the table, solving the problem is trivial.
If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.
Any time you get the...
"Fatal error: Call to a member function bind_param() on boolean"
...it is likely because there is an issue with your query. The prepare()
might return FALSE
(a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!
First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log
. If you're examining error logs in a Linux environment you can use tail -f /path/to/log
in a console window to see errors as they occur in real-time....or as you make them.
Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
The error is generic and not very helpful to you in solving what is going on.
With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare()
statement for truthiness and if it is good you can proceed on to binding and executing.
$sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
$query->bind_param('s', $definition);
$query->execute();
// any additional code you need would go here.
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
}
If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo
column in the table, solving the problem is trivial.
If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.
answered Aug 5 '15 at 21:01
Jay Blanchard
35.3k125495
35.3k125495
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
add a comment |
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
4
4
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
– Dawid Ferenczy
Nov 27 '15 at 17:04
add a comment |
up vote
11
down vote
prepare
return a boolean only when it fails therefore FALSE
, to avoid the error you need to check if it is True
first before executing:
$sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
if($query = $this->db->conn->prepare($sql)){
$query->bind_param('s', $setting);
$query->execute();
//rest of code here
}else{
//error !! don't go further
var_dump($this->db->error);
}
add a comment |
up vote
11
down vote
prepare
return a boolean only when it fails therefore FALSE
, to avoid the error you need to check if it is True
first before executing:
$sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
if($query = $this->db->conn->prepare($sql)){
$query->bind_param('s', $setting);
$query->execute();
//rest of code here
}else{
//error !! don't go further
var_dump($this->db->error);
}
add a comment |
up vote
11
down vote
up vote
11
down vote
prepare
return a boolean only when it fails therefore FALSE
, to avoid the error you need to check if it is True
first before executing:
$sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
if($query = $this->db->conn->prepare($sql)){
$query->bind_param('s', $setting);
$query->execute();
//rest of code here
}else{
//error !! don't go further
var_dump($this->db->error);
}
prepare
return a boolean only when it fails therefore FALSE
, to avoid the error you need to check if it is True
first before executing:
$sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
if($query = $this->db->conn->prepare($sql)){
$query->bind_param('s', $setting);
$query->execute();
//rest of code here
}else{
//error !! don't go further
var_dump($this->db->error);
}
answered Dec 10 '14 at 6:48
![](https://i.stack.imgur.com/ARiD0.gif?s=32&g=1)
![](https://i.stack.imgur.com/ARiD0.gif?s=32&g=1)
meda
39.1k1166109
39.1k1166109
add a comment |
add a comment |
up vote
6
down vote
Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
Always close your previous statement with
$statement->close();
If the syntax is correct, the following query will run well too.
This answer helped me.
– Amine
Jan 4 at 17:23
add a comment |
up vote
6
down vote
Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
Always close your previous statement with
$statement->close();
If the syntax is correct, the following query will run well too.
This answer helped me.
– Amine
Jan 4 at 17:23
add a comment |
up vote
6
down vote
up vote
6
down vote
Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
Always close your previous statement with
$statement->close();
If the syntax is correct, the following query will run well too.
Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
Always close your previous statement with
$statement->close();
If the syntax is correct, the following query will run well too.
answered May 24 '17 at 8:08
Tamas Kalman
1,25411523
1,25411523
This answer helped me.
– Amine
Jan 4 at 17:23
add a comment |
This answer helped me.
– Amine
Jan 4 at 17:23
This answer helped me.
– Amine
Jan 4 at 17:23
This answer helped me.
– Amine
Jan 4 at 17:23
add a comment |
up vote
1
down vote
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 of Tablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.
add a comment |
up vote
1
down vote
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 of Tablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.
add a comment |
up vote
1
down vote
up vote
1
down vote
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 of Tablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.
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 of Tablename
. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.
edited Sep 22 '17 at 13:54
Cody Gray♦
190k34370459
190k34370459
answered Sep 22 '17 at 12:04
![](https://i.stack.imgur.com/inwkA.png?s=32&g=1)
![](https://i.stack.imgur.com/inwkA.png?s=32&g=1)
4ndyG
307
307
add a comment |
add a comment |
up vote
0
down vote
Sometimes, it is also because of a wrong table name or column name in the prepare statement.
See this:
https://www.youtube.com/watch?v=LGHdQ1bBho8
add a comment |
up vote
0
down vote
Sometimes, it is also because of a wrong table name or column name in the prepare statement.
See this:
https://www.youtube.com/watch?v=LGHdQ1bBho8
add a comment |
up vote
0
down vote
up vote
0
down vote
Sometimes, it is also because of a wrong table name or column name in the prepare statement.
See this:
https://www.youtube.com/watch?v=LGHdQ1bBho8
Sometimes, it is also because of a wrong table name or column name in the prepare statement.
See this:
https://www.youtube.com/watch?v=LGHdQ1bBho8
answered Feb 17 at 11:40
![](https://lh3.googleusercontent.com/-WNwGOqi98xc/AAAAAAAAAAI/AAAAAAAAALQ/Mxny-Hg7VAk/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-WNwGOqi98xc/AAAAAAAAAAI/AAAAAAAAALQ/Mxny-Hg7VAk/photo.jpg?sz=32)
Reejesh PK
7127
7127
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Mar 27 at 14:49
Sam Banana
1918
1918
add a comment |
add a comment |
up vote
0
down vote
This particular error has very little to do with the actual error. Here is my similar experience and the solution...
I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...
add a comment |
up vote
0
down vote
This particular error has very little to do with the actual error. Here is my similar experience and the solution...
I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...
add a comment |
up vote
0
down vote
up vote
0
down vote
This particular error has very little to do with the actual error. Here is my similar experience and the solution...
I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...
This particular error has very little to do with the actual error. Here is my similar experience and the solution...
I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...
answered Mar 31 at 7:40
Sam Saarian
317212
317212
add a comment |
add a comment |
up vote
0
down vote
Following two are the most probable cause of this issue:
- Spelling mistake either in column names or table name
- Previously established statement is not closed. Just close it before making the prepared statement.
$stmt->close(); // <<<-----This fixed the issue for me
$stmt = $conn->prepare("Insert statement");
add a comment |
up vote
0
down vote
Following two are the most probable cause of this issue:
- Spelling mistake either in column names or table name
- Previously established statement is not closed. Just close it before making the prepared statement.
$stmt->close(); // <<<-----This fixed the issue for me
$stmt = $conn->prepare("Insert statement");
add a comment |
up vote
0
down vote
up vote
0
down vote
Following two are the most probable cause of this issue:
- Spelling mistake either in column names or table name
- Previously established statement is not closed. Just close it before making the prepared statement.
$stmt->close(); // <<<-----This fixed the issue for me
$stmt = $conn->prepare("Insert statement");
Following two are the most probable cause of this issue:
- Spelling mistake either in column names or table name
- Previously established statement is not closed. Just close it before making the prepared statement.
$stmt->close(); // <<<-----This fixed the issue for me
$stmt = $conn->prepare("Insert statement");
answered May 1 at 19:10
![](https://i.stack.imgur.com/Duik4.png?s=32&g=1)
![](https://i.stack.imgur.com/Duik4.png?s=32&g=1)
Hari Das
2,96832535
2,96832535
add a comment |
add a comment |
up vote
0
down vote
Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?)
may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)
add a comment |
up vote
0
down vote
Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?)
may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)
add a comment |
up vote
0
down vote
up vote
0
down vote
Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?)
may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)
Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?)
may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)
edited May 20 at 18:06
![](https://i.stack.imgur.com/seFjX.png?s=32&g=1)
![](https://i.stack.imgur.com/seFjX.png?s=32&g=1)
Lab Lab
346321
346321
answered May 20 at 17:32
ebite Zion
508
508
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27394710%2ffatal-error-call-to-a-member-function-bind-param-on-boolean%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
IWFPSEufjd,IrGO kUJ 2GIUghMx6zVGACxlQi9xJ26Ud8 XRs VX3X,58g TCvLfV2fRZ4,twYI yMX6H6fKPh W sBBTsc,FR,i9