MySQLi Update Throwing Call to a Member Function Bind_Param() Error

Mysqli update throwing Call to a member function bind_param() error

O, let's try a canonical answer.

Call to a member function (or expects parameter 1 to be mysqli_result, boolean given for the procedural style) is not an error itself but just a symptom, for some other problem.

This very error message means that no object was created where should.

So - there was a problem with creating an $stmt object.

Most likely it's a problem with the query.
So, we need to track that error down.

Mysqli won't tell you what's going on unless asked explicitly. So, you have to always check the result of every mysqli function interacting with server and if result is FALSE - check $mysqli->error.

It is also very important to convert mysqli error message into PHP error, to let it go according site-wide error reporting settings.

If you are using mysqli_query() all over the application code without encapsulating it into some helper class, trigger_error() is a good way to raise a PHP error, as it will tell you also the file and the line number where error occurred

So, all your prepare(), execute() and query() calls have to be written this way:

$stmt = $mysqli->prepare($query) or trigger_error($mysqli->error."[$query]");

or in procedural style

$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");

in all your scripts

and since then you will be notified of the reason, why the object weren't created.
(If you're curious of this or syntax, I've explained it here)
Note that query also included in the error message, to let you inspect it visually and test in another environment.

However, if you're encapsulating your query into some class, file and line from trigger error will be quite useless as they will point to the call itself, not the application code that caused certain problem. So, when running mysqli commands encapsulated, another way have to be used:

$result = $mysqli->query($sql);
if (!$result) {
throw new Exception($mysqli->error." [$query]");
}

as Exception will provide you with a stack trace, which will lead you the the place from which an erroneous query were called.

Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

while on a local development server it's all right to make errors on screen:

error_reporting(E_ALL);
ini_set('display_errors',1);

and of course you should never ever use error suppression operator (@) in front of your statements.

Call to a member function bind_param() on boolean error in PHP--MySQL

First turn on error reporting by adding the below two lines at the top of your page. Then try printing out the exact error by doing echo $mysli->error;

error_reporting(E_ALL);
ini_set('display_errors', 1);

$mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$statement = $mysqli->prepare("INSERT INTO brickTable (url, description) VALUES(?,?)");
echo $mysqli->error;
$statement->bind_param("ss", $_POST["url"], $_POST["description"]);
$statement->execute();

It will tell you the error.

On a function that gets settings from a DB I ran into the error

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.)

PHP Fatal error: Call to a member function bind_param()

Since nobody else has spotted the issue, I'll post it for you. The reason you're prepare() is failing is because you're trying to use a MySQL Reserved Word. The word desc is a reserved word in MYSQL, which means you need to wrap it in backticks like this:

$statement= $db->prepare("insert into uploaddetails(idnum,title,`desc`,author,tags,file) values(?,?,?,?,?,?)");

It also helps to use proper practice when inserting into a database/using prepared statements.

$statement= $db->prepare("insert into uploaddetails(idnum,title,`desc`,author,tags,title) values(?,?,?,?,?,?)");

if($statement !== FALSE) {
// do the binds...etc
}

Notes

file is also a reserved word, I don't know what your actual file columns name is, so keep that in mind.

(Fatal error: Call to a member function bind_param() on a non-object)

  $eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon,       $shortdesc, $genlink); // line 44

You need to the define the type of parameters as this:

$eintrag->bind_param("ssssssiiss", $titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44

s - string
i - int
Also check documentation: http://php.net/manual/en/mysqli-stmt.bind-param.php

PHP - Fatal error: Call to a member function bind_param()

TL\DR

Your query is failing to prepare(). You need to figure out where, how and why. Look at the last code block of this answer and let us know what the error is.


I'll start with the query. You're trying to access a MySQL reserved word Source (See #684). You need to wrap those in backticks like this:

$add = "INSERT INTO books (title, edited, created, ip,".
" email_to, twitter, last_taken, questions_total, responses, ".
"show_progress, need_correct, go_back, state, send_stats, ".
"show_number, imported) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ".
"?, ?, ?, ?, ?, ?, ?)";

Now, you're instantiating the variable $stmt within the if block but then trying to bind it outside of that block. You'll need to change this:

if ($stmt = $mysqli->prepare($add)) {
....
}
$stmt->bind_param(....);

To this:

if ($stmt = $mysqli->prepare($add)) {
....
$stmt->bind_param(....);
}

Also, make sure your query is actually preparing correctly:

if ($stmt = $mysqli->prepare($add)) {

$stmt->bind_param("siisssiiiiiiiiii", $title, $edited, $created, $ip, $email_to, $twitter, $last_taken, $questions_total, $responses, $show_progress, $need_correct, $go_back, $state, $send_stats, $show_number, $importedVal);

// execute it and all...
} else {
die("Errormessage: ". $mysqli->error);
}

Then let us know what turns up.

mySQLi insert error - Call to a member function bind_param

$stmt = $mysqli->prepare('INSERT INTO users (id, firstname, lastname) VALUES ("", ?, ?)');

Removed ! in from of $stmt and you were missing a ) to close the VALUES

How to deal with mysqli problems? mysqli_fetch_array(): Argument #1 must be of type mysqli_result

TL;DR

  1. Always have mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); in your mysqli connection code and always check the PHP errors.
  2. Always replace every PHP variable in the SQL query with a question mark, and execute the query using prepared statement. It will help to avoid syntax errors of all sorts.

Explanation

Sometimes your MySQLi code produces an error like mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given..., Call to a member function bind_param()... or similar. Or even without any error, but the query doesn't work all the same. It means that your query failed to execute.

Every time a query fails, MySQL has an error message that explains the reason. In the older PHP versions such errors weren't transferred to PHP, and all you'd get is a cryptic error message mentioned above. Hence it is very important to configure PHP and MySQLi to report MySQL errors to you. And once you get the error message, fixing it will be a piece of cake.

How to get the error message in MySQLi

First of all, always have this line before MySQLi connect in all your environments:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

After that, all MySQL errors will be transferred into PHP exceptions. An uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. And the stack trace will lead you to the exact spot where the error occurred.

How to get the error message from PHP

Here is a gist of my article on PHP error reporting:
Reporting errors on a development and live servers must be different. On the development server it is convenient to have errors shown on-screen, but on a live server error messages must be logged instead, so you could find them in the error log later.

Therefore, you must set corresponding configuration options to the following values:

  • On a development server

  • error_reporting should be set to E_ALL value;

  • log_errors should be set to 1 (it is convenient to have logs on a development PC too)

  • display_errors should be set to 1

  • On a production server

  • error_reporting should be set to E_ALL value;

  • log_errors should be set to 1

  • display_errors should be set to 0

After that, when MySQL query fails, you will get a PHP error that explains the reason. On a live server, in order to get the error message, you'll have to check the error log.

How to actually use it

Just remove any code that checks for the error manually, all those or die(), if ($result), try..catch and such. Simply write your database interaction code right away:

$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");
$stmt->bind_param("si", $name, $quantity);
$stmt->execute();

Again, without any conditions around. If an error occurs, it will be treated like any other error in your code. For example, on a development PC it will just appear on-screen, while on a live site it will be logged for the programmer, whereas for the user's convenience you could use an error handler (but that's a different story which is off topic for MySQLi, but you may read about it in the article linked above).

What to do with the error message you get

First of all you have to locate the problem query. The error message contains the file name and the line number of the exact spot where the error occurred. For the simple code that's enough, but if your code is using functions or classes you may need to follow the stack trace to locate the problem query.

After getting the error message, you have to read and comprehend it. It sounds too obvious if not condescending, but learners often overlook the fact that the error message is not just an alarm signal, but it actually contains a detailed explanation of the problem. And all you need is to read the error message and fix the issue.

  • Say, if it says that a particular table doesn't exist, you have to check spelling, typos, and letter case. Also you have to make sure that your PHP script connects to a correct database
  • Or, if it says there is an error in the SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error message.

If you don't understand the error message, try to google it. And when browsing the results, stick to answers that explain the error rather than bluntly give the solution. A solution may not work in your particular case, but the explanation will help you to understand the problem and make you able to fix the issue by yourself.

You have to also trust the error message. If it says that number of tokens doesn't match the number of bound variables then it is so. The same goes for the absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.

A list of things you should never ever do in regard of error reporting

  • Never use an error suppression operator (@)! It makes a programmer unable read the error message and therefore unable to fix the error
  • Do not use die() or echo or any other function to print the error message on the screen unconditionally. PHP can report errors by itself and do it the right way depends on the environment - so just leave it for PHP.
  • Do not add a condition to test the query result manually (like if($result)). With error exceptions enabled such condition will just be useless.
  • Do not use the try..catch operator for echoing the error message. This operator should be used to perform some error handling, like a transaction rollback. But never use it just to report errors - as we learned above, PHP can already do it, the right way.

P.S.

Sometimes there is no error, but no results either. Then it means, there is no data in the database to match your criteria. In this case you have to admit this fact, even if you can swear the data and the criteria are all right. They are not. You have to check them again.

I've got an article that can help in this matter, How to debug database interactions. Although it is written for PDO, the principle is the same. Just follow those instructions step by step and either have your problem solved or have an answerable question for Stack Overflow.



Related Topics



Leave a reply



Submit