-2

I have been trying to convert code over to using mysqli prepared statements (Converting from mysqli to prepared statements). I have the following code which I can not silence the error message from the call. Instead it is spitting out the "PHP Fatal error: ..." which halts the script so the error code is not executing but because the error is spitting out, only the header is being shown to the user.

Also note, I was attempting to also convert over to using the native php error handling as described by 'Your Common Sense' in the referenced post, but reverted back to the traditional "if (! ...) {...}" style of code due to issues not mentioned by that person (by doing that conversion). All of these modifications have been reversed that I can see.

Now for the code (and note that there is an intentional misnaming of the DB table to throw an error for checking of the code)...

$SQL = "SELECT * FROM Issues2 WHERE id=? AND disabled='0' LIMIT 1";
if (! $PRE = @mysqli_prepare($linkDB, $SQL)) {
    echo "<f><msg>ERROR: Could not prepare query: ".$SQL.", ".mysqli_error($linkDB)."</msg></f>";
} else {
    ...
}

I have tried calling the mysqli_prepare() call with and without the prepended '@' symbol, but no luck... I should be getting the "ERROR: Could not prepare ..." message, but instead I just get the XML header alone. Any help would be appreciated!

user1646428
  • 179
  • 2
  • 12
  • 3
    I would suggest actually fixing the issue instead of suppressing it. try/catch blocks are great for catching fatal errors so that you can log them and then clean up as needed. – aynber Jul 30 '19 at 19:09
  • Please provide the full error message of the fatal error so that we can help you fix it. – Patrick Q Jul 30 '19 at 19:16
  • @aynber Maybe I'm wrong, but I would say that try/catch blocks are _not_ great for catching fatal errors. – Patrick Q Jul 30 '19 at 19:19
  • @PatrickQ Hmm, no, not for fatal errors. The actual error message really would help out here. :/ – aynber Jul 30 '19 at 19:22
  • Are you using `mysqli_report()` to generate automatic error reports? It probably can't be suppressed with `@` like built-in functions can. You'll need to use `mysqli_report(MYSQLI_REPORT_OFF);` to temporarily disable it. – Barmar Jul 30 '19 at 19:44
  • Thanks for all the replies, but I think you all missed the sentence "and note that there is an intentional misnaming of the DB table to throw an error for checking of the code". I am trying to get the code to work so in the event of an actual error of some sort it doesn't show errors and such to the user, but dies in a graceful manor while notifying our staff. – user1646428 Jul 31 '19 at 13:05
  • @Barmar unless mysqli_report() is something enabled by default, no I did not enable it. I'll try turning it off to see if that does anything... – user1646428 Jul 31 '19 at 13:09
  • @Barmar that worked! If you want to put that in an answer, I'll mark it as the accepted answer. Any idea why this would be on? Not sure if it is something that got turned on by trying to implement the error checking by 'Your Common Sense' that didn't get turned back off... – user1646428 Jul 31 '19 at 13:23
  • Turns out it was a line that was not commented out from the attempts at using 'Your Common Sense' error handling (which employed a mysqli_report() call). I have commented it out and all appears to be working as it should. – user1646428 Jul 31 '19 at 14:25
  • As a side note, for anyone coming to this post in the future. By commenting out the "mysqli_report()" call as suggested by Dharman in the referenced SO link in the OP, the error handling suggested by 'Your Common Sense' works (although it does require a little more code than originally suggested which he says he is updating his website that he referenced). Just an FYI... – user1646428 Jul 31 '19 at 14:58

1 Answers1

1

You must be using

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

to enable automatic error reporting from mysqli functions; otherwise, they just return a falsey value and you have to check for this in your code. Unlike built-in PHP functions, @ doesn't suppress these error reports.

Use

mysqli_report(MYSQLI_REPORT_OFF);
// do stuff with explicit error checking
// ...
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if you want to temporarily disable this. Or remove the above call if you want to revert to explicit error checking everywhere.

Barmar
  • 741,623
  • 53
  • 500
  • 612