Monday, August 20, 2018

php - mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?




In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:




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




Here is the code:



global $mysqli;

$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);


To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.


Answer



Sometimes your mysqli code produces an error like mysqli_fetch_assoc() expects parameter, 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. Unfortunately, by default, such errors are not transferred to PHP, and all you have is a cryptic error message mentioned above. Hence it is very important to configure PHP and mysqli to report you MySQL errors. And once you get the error message, it will be a piece of cake to fix the issue.



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. 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 a stack trace will lead you to the exact spot where error occurred.




How to configure PHP in different environments



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



You must set the 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 dev 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




How to actually use it?



Just remove any code that checks for the error manually, all those or die(), if ($result) 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 as any other error in your code. For example, on a development PC it just appear on the screen, while on a live site it will be logged for a programmer, whereas for the user's convenience you could use an error wrapper but that's a different story which is about off topic for mysqli, but you may read about it in the article linked above.



What to do with the error message you get



After getting the error message, you have to read and comprehend it. It sounds too obvious, but learners often overlook the extreme helpfulness of the error message. Yet most of time it explains the problem pretty straightforward. Say, if it says that particular table doesn't exist, you have to check spelling, typos, letter case, credentials and such. Or, if it says there is an error in 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 massage 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 it 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 what you should never 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 will report it by itself and do it the right way depends on the environment.

  • Do not add a condition to test the query result manually (like if($result)). Either your query failed and you will already get the error exception, or it was all right and there is nothing to test.

  • Do not use 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, but the principle is the same. Just follow this instruction step by step and either have your problem solved or have an answerable question for .


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...