By siopaoman

2014-03-26 13:27:38 8 Comments

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->bind_result($uid, $desc);

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


@Your Common Sense 2014-03-26 13:32:24

Sometimes your query fails and you don't know why. Hence it is very important to configure PHP and mysqli to report you every error.

How to get the error message in mysqli

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

How to configure PHP in different environments

Here is a gist of my article on PHP error reporting:
Note that you have to be able to see PHP errors in general. And here indeed goes the matter of different environments:

You must set the corresponding configuration options to the following values

  • On a development server

    • error_reporting should be set to E_ALL value;
    • display_errors should be set to 1
  • On a production server

    • error_reporting should be set to E_ALL value;
    • display_errors should be set to 0
    • log_errors should be set to 1

How to actually use it?

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

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

again, witohut any conditions around. If an error occurs, then it will be treated as any other error in your code. For example, on a development PC it sill just appear on screen. And for a live site you will need an error wrapper but that's a different story, completely irrelevant to mysqli and its errors.

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

You have to also trust the error message. If it says that number of tokens doesn't match 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 eporting

  • 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 is required.
  • Testing the query result manually (like if($result)) 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 the error message. Again PHP can do it better, way better.

@siopaoman 2014-03-26 13:41:55

That was fast. Thanks for the inputs masterful guru. I found the cause. Though it did not directly fix the problem, it lead me to the fix and I learned a very helpful technique from you too.

@Your Common Sense 2014-03-26 13:43:28

Out of curiosity, which one of the above guesses accidentally won?

@siopaoman 2014-03-26 13:46:09

All of the above inputs helped me. But your answer specifically zoomed on the cause. Thanks to all of you. :)

@Amal Murali 2014-03-26 13:50:52

@siopaoman: What turned out to be the issue?

@siopaoman 2014-03-26 15:23:32

@Amal Murali: I made some recent changes on some table fields and it is not quite the same with what mysqli is seeing. >Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Unknown column 'cur_own_id' in 'where clause'' in

@Your Common Sense 2017-10-23 07:55:02

@aendeerei it is already there, with the focus on the error reporting in general. But I don't see you are following my recommendations in your answers, so I don't understand why you're so excited about them

@Your Common Sense 2017-10-23 08:08:18

@aendeerei you still don't get the main point of the article: just leave error messages alone. The site is good without your handling code already. It is much better without your handling code and comments telling to remove the echo. PHP already does it

@Your Common Sense 2017-10-23 08:09:47

@aendeerei "But that user needed a whole example of how his code should be optimized." such questions are off topic on Stack Overflow. they belong to codereview site.

@Your Common Sense 2017-10-23 08:14:18

@aendeerei I don't monitor your answers. it's you came to comment my answer here telling me that my articles are good, but at the same time, as I can see, you don't follow them. the code in your code review is redundant: you set display_errors at the top AND echo the error message at the bottom. WHY you are doing the second if PHP already does it?

@Your Common Sense 2017-10-23 08:20:47

@aendeerei no, it's off topic for their question. All they need is a proper code that can be later used in any environment. If you don't trust me, please go to Meta and ask a question, "Should I write a full code review including PHP, mysql, HTML and Bootstrap in response to a "my code doesn't work" question.

@Your Common Sense 2017-10-23 08:22:52

@aendeerei exceptions and fatal errors are THE SAME. this is the point of all my articles

@Your Common Sense 2017-10-23 08:35:16

@aendeerei if there is no special action in case of exception, like rolling back a transaction, or telling a user that such username already exists, then you shouldn't catch it. just let it go. if not caught, it will be converted to PHP error and processed accordingly. so just add basic error reporting commands like error_reporting() and display_errors.

@Your Common Sense 2017-10-23 08:36:06

@aendeerei right. If you like to write such full reviews, then register on

@Your Common Sense 2017-10-23 08:37:54

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How do I get PHP errors to display?

2 Answered Questions

[SOLVED] mysqli: can it prepare multiple queries in one statement?

0 Answered Questions

Cannot bind parameters to mysqli prepared statement

1 Answered Questions

[SOLVED] Issue with "Call to member function on non-object"

1 Answered Questions

Simple select query gives error

  • 2015-04-04 10:47:45
  • Krooy_mans
  • 119 View
  • 0 Score
  • 1 Answer
  • Tags:   php mysqli

2 Answered Questions

[SOLVED] bind_param Failing But Echo the bind_results return what I want?

  • 2013-01-15 21:30:30
  • user1968541
  • 111 View
  • -1 Score
  • 2 Answer
  • Tags:   php mysqli

1 Answered Questions

[SOLVED] mysqli bind_param not working

  • 2012-12-14 09:45:53
  • Mar Cejas
  • 1448 View
  • 0 Score
  • 1 Answer
  • Tags:   mysqli

1 Answered Questions

0 Answered Questions

error on mysqli prepare

  • 2011-09-22 04:00:05
  • Greg
  • 95 View
  • 0 Score
  • 0 Answer
  • Tags:   php mysql mysqli

3 Answered Questions

Sponsored Content