How to get mysqli error in different environments?

  • Added:
  • |
  • In: Basic PHP

In my LOCAL/DEV environment, the mysqli query is performing OK. However, when I upload it on my WEBHOST environment, I got 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->bind_result($uid, $desc);

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

If there are any lacking information, please tell me so that I can immediately provide.

This Question Has 1 Answeres | Orginal Question | siopaoman

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


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

Note that you have to be able to see PHP errors in general. And here indeed goes the matter of different environments:

  • on a live site you have to peek into error logs, so, settings have to be

  • while on a local development server it's ok to make errors on screen:


And a little list of what you should not:

  • use error suppression operator (@)
  • use die() or echo or any other function to print the error message on screen unconditionally. PHP can echo it all right already, no assistance required.
  • test the query result manually (like if($result)) it just makes no sense. Either your query failed and you will already get the error exception, or it was all right and there is nothing to test
  • use try..catch for echoing error message. Again PHP can do it better, way better.

Meet with owner

Sajjad Hossain

Hey, I am Sajjad, working in web development sector since 2012. I love to do amazing things. Let's do a project together.
Connect Social With PHPAns