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

1 comments

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

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

    error_reporting(E_ALL);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);
    
  • While on a local development server it's OK to make errors on the screen:

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

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 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, phpdelusions.net/articles/error_reporting 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 codereview.stackexchange.com

@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?

31 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

2 Answered Questions

[SOLVED] bind_param() error in mysqli query

3 Answered Questions

[SOLVED] MySQLi Error for Query

2 Answered Questions

[SOLVED] MySQLi Call to a member function bind_param() on a non-object Error

  • 2012-09-26 21:14:47
  • Jopc67
  • 4264 View
  • 1 Score
  • 2 Answer
  • Tags:   php mysql mysqli

1 Answered Questions

1 Answered Questions

[SOLVED] an error and warning appearing in my php/mysqli

  • 2012-08-24 20:35:57
  • user1394925
  • 81 View
  • 0 Score
  • 1 Answer
  • Tags:   php mysqli

0 Answered Questions

error on mysqli prepare

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

3 Answered Questions

Sponsored Content