How to get MySQLi error information in different environments












68















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.










share|improve this question

























  • Can we see where are you initiating $mysqli variable ?

    – Rikesh
    Mar 26 '14 at 13:29











  • It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

    – Amal Murali
    Mar 26 '14 at 13:30











  • What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

    – N.B.
    Mar 26 '14 at 13:30
















68















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.










share|improve this question

























  • Can we see where are you initiating $mysqli variable ?

    – Rikesh
    Mar 26 '14 at 13:29











  • It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

    – Amal Murali
    Mar 26 '14 at 13:30











  • What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

    – N.B.
    Mar 26 '14 at 13:30














68












68








68


17






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.










share|improve this question
















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.







php mysqli prepared-statement environment error-reporting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 27 '18 at 11:57









Peter Mortensen

13.7k1986113




13.7k1986113










asked Mar 26 '14 at 13:27









siopaomansiopaoman

34334




34334













  • Can we see where are you initiating $mysqli variable ?

    – Rikesh
    Mar 26 '14 at 13:29











  • It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

    – Amal Murali
    Mar 26 '14 at 13:30











  • What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

    – N.B.
    Mar 26 '14 at 13:30



















  • Can we see where are you initiating $mysqli variable ?

    – Rikesh
    Mar 26 '14 at 13:29











  • It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

    – Amal Murali
    Mar 26 '14 at 13:30











  • What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

    – N.B.
    Mar 26 '14 at 13:30

















Can we see where are you initiating $mysqli variable ?

– Rikesh
Mar 26 '14 at 13:29





Can we see where are you initiating $mysqli variable ?

– Rikesh
Mar 26 '14 at 13:29













It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

– Amal Murali
Mar 26 '14 at 13:30





It could be that your MySQL user is lacking the privileges to do a SELECT query. Did you check that?

– Amal Murali
Mar 26 '14 at 13:30













What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

– N.B.
Mar 26 '14 at 13:30





What pops to mind is that there's no mysqli available or you provided wrong credentials to connect to MySQL.

– N.B.
Mar 26 '14 at 13:30












2 Answers
2






active

oldest

votes


















75














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.






share|improve this answer


























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

    – siopaoman
    Mar 26 '14 at 13:41








  • 1





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

    – Your Common Sense
    Mar 26 '14 at 13:43











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

    – siopaoman
    Mar 26 '14 at 13:46






  • 2





    @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
    Oct 23 '17 at 8:20






  • 1





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

    – Your Common Sense
    Oct 23 '17 at 8:22





















0














I don't think you have created the object.



$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');



but I really suggest you use PDO instead.






share|improve this answer



















  • 2





    This seems to be more of a comment and not a resolution. :)

    – BlooB
    Oct 11 '18 at 14:20











  • I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

    – Wayne Oliver
    Oct 11 '18 at 20:37











  • Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

    – O. Jones
    Oct 11 '18 at 23:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22662488%2fhow-to-get-mysqli-error-information-in-different-environments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









75














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.






share|improve this answer


























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

    – siopaoman
    Mar 26 '14 at 13:41








  • 1





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

    – Your Common Sense
    Mar 26 '14 at 13:43











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

    – siopaoman
    Mar 26 '14 at 13:46






  • 2





    @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
    Oct 23 '17 at 8:20






  • 1





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

    – Your Common Sense
    Oct 23 '17 at 8:22


















75














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.






share|improve this answer


























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

    – siopaoman
    Mar 26 '14 at 13:41








  • 1





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

    – Your Common Sense
    Mar 26 '14 at 13:43











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

    – siopaoman
    Mar 26 '14 at 13:46






  • 2





    @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
    Oct 23 '17 at 8:20






  • 1





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

    – Your Common Sense
    Oct 23 '17 at 8:22
















75












75








75







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 27 '18 at 12:00









Peter Mortensen

13.7k1986113




13.7k1986113










answered Mar 26 '14 at 13:32









Your Common SenseYour Common Sense

1




1













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

    – siopaoman
    Mar 26 '14 at 13:41








  • 1





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

    – Your Common Sense
    Mar 26 '14 at 13:43











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

    – siopaoman
    Mar 26 '14 at 13:46






  • 2





    @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
    Oct 23 '17 at 8:20






  • 1





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

    – Your Common Sense
    Oct 23 '17 at 8:22





















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

    – siopaoman
    Mar 26 '14 at 13:41








  • 1





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

    – Your Common Sense
    Mar 26 '14 at 13:43











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

    – siopaoman
    Mar 26 '14 at 13:46






  • 2





    @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
    Oct 23 '17 at 8:20






  • 1





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

    – Your Common Sense
    Oct 23 '17 at 8:22



















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.

– siopaoman
Mar 26 '14 at 13:41







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.

– siopaoman
Mar 26 '14 at 13:41






1




1





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

– Your Common Sense
Mar 26 '14 at 13:43





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

– Your Common Sense
Mar 26 '14 at 13:43













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

– siopaoman
Mar 26 '14 at 13:46





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

– siopaoman
Mar 26 '14 at 13:46




2




2





@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
Oct 23 '17 at 8:20





@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
Oct 23 '17 at 8:20




1




1





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

– Your Common Sense
Oct 23 '17 at 8:22







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

– Your Common Sense
Oct 23 '17 at 8:22















0














I don't think you have created the object.



$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');



but I really suggest you use PDO instead.






share|improve this answer



















  • 2





    This seems to be more of a comment and not a resolution. :)

    – BlooB
    Oct 11 '18 at 14:20











  • I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

    – Wayne Oliver
    Oct 11 '18 at 20:37











  • Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

    – O. Jones
    Oct 11 '18 at 23:29
















0














I don't think you have created the object.



$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');



but I really suggest you use PDO instead.






share|improve this answer



















  • 2





    This seems to be more of a comment and not a resolution. :)

    – BlooB
    Oct 11 '18 at 14:20











  • I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

    – Wayne Oliver
    Oct 11 '18 at 20:37











  • Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

    – O. Jones
    Oct 11 '18 at 23:29














0












0








0







I don't think you have created the object.



$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');



but I really suggest you use PDO instead.






share|improve this answer













I don't think you have created the object.



$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');



but I really suggest you use PDO instead.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 11 '18 at 14:07









Wayne OliverWayne Oliver

112




112








  • 2





    This seems to be more of a comment and not a resolution. :)

    – BlooB
    Oct 11 '18 at 14:20











  • I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

    – Wayne Oliver
    Oct 11 '18 at 20:37











  • Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

    – O. Jones
    Oct 11 '18 at 23:29














  • 2





    This seems to be more of a comment and not a resolution. :)

    – BlooB
    Oct 11 '18 at 14:20











  • I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

    – Wayne Oliver
    Oct 11 '18 at 20:37











  • Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

    – O. Jones
    Oct 11 '18 at 23:29








2




2





This seems to be more of a comment and not a resolution. :)

– BlooB
Oct 11 '18 at 14:20





This seems to be more of a comment and not a resolution. :)

– BlooB
Oct 11 '18 at 14:20













I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

– Wayne Oliver
Oct 11 '18 at 20:37





I think the error message makes it fairly clear what the problem is :-) But I hear you. There are definitely a few other bits of advice we could provide here too.

– Wayne Oliver
Oct 11 '18 at 20:37













Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

– O. Jones
Oct 11 '18 at 23:29





Welcome to Stack Overflow. If you flesh out this answer it will be more useful.

– O. Jones
Oct 11 '18 at 23:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22662488%2fhow-to-get-mysqli-error-information-in-different-environments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen