-1

in this case I accidentally made a wrong database username or password, my goal here is how to generate an error in the form of JSON data and open in the form of a PHP warning or error like my picture

Error Image

// Here's my PHP Code

if (!empty($_POST)){
   $test_conn = new mysqli('localhost' , $_POST['db_user'] , $_POST['db_pass']);
   $conn = new mysqli('localhost' , $_POST['db_user'] , $_POST['db_pass'] , $_POST['db_name']);
   if ($test_conn->connect_error){
       $response = array('status' => 0 , 'message' => "Failed Connect to Databases");
       print_r(json_encode($response));
   }else{
       // The Code When Username and Password is Correct
   }
}

here I know that the username or password is wrong, but I am confused how to only display the json data in the image at the bottom and not display the php warning

MR. A
  • 195
  • 1
  • 11
  • Dont know what you try to accomplish, but you shouldnt submit db credentials through a form. – Ben Dec 25 '18 at 12:05
  • yeah , i know it @Ben , this page only for admin , i make a setup page, where admin can make automatically database and tables only input username and password – MR. A Dec 25 '18 at 12:21
  • please have a look at https://stackoverflow.com/questions/15553496/new-mysqli-how-to-intercept-an-unable-to-connect-error – Ben Dec 25 '18 at 19:48

2 Answers2

0

If you are sure that this is what you want, just disable errors by adding the following at the top of your script:

error_reporting(0);

if you want to hide errors for a given code, use the following:

// Disable errors and get current error reporting level
$err = error_reporting(0);

/* here goes you buggy code */

// Set the default error reporting level
error_reporting($err);
Victor
  • 5,493
  • 1
  • 27
  • 28
  • This is work for me . But this is disable all error report right ? – MR. A Dec 25 '18 at 11:51
  • how to enable it ? just make it (1) ? – MR. A Dec 25 '18 at 11:53
  • @AjiDwiPrasetio, Yes, it disables all errors. If you want to disable them only for a piece of code, check my update. – Victor Dec 25 '18 at 11:57
  • 2
    If page is reachable through www you must disable display_errors ! Its a big security advantage. – Ben Dec 25 '18 at 12:10
  • 1
    @digijay Ben is right. On a production server you should not display errors. This is why using the error control operator (`@`) is a bad idea, because it doesn’t offer you any flexibility. For example, on localhost you may want to display errors, while on the remote server you should hide them. Given this, you cannot add `@` every time you need to upload files on the production server. The `error_reporting()` is the best option at least because you can control where and for whom you can show errors (for example, `if (not_admin() || not_localhost()) {error_reporting(0);}`). – Victor Dec 25 '18 at 12:34
  • @Victor: yes, absolutly! `display_errors` should always be disabled, with error messages only going to error_log. Thanks for pointing that out, @Ben! But I disagree with "Errors should never pass silently" because you won't get error messages to process in an callback of a ajax call. – digijay Dec 25 '18 at 12:49
  • @digijay So why you said *you shouldn't do this*? ;) Anyway, I totally agree, that there are several cases, when you don’t want to save or display _garbage errors_. – Victor Dec 25 '18 at 13:16
-1

You have to suppress the php error reporting in order to use your own check of connect_error. The php documentation on mysqli::$connect_error suggests to do this with the @ error control operator like this:

$test_conn = @new mysqli( ... )

This will only suppress errors caused by a faulty instantiation of the mysqli object. It should be clear that you then have to handle these errors yourself (as you already do). All other errors still could cause your php script to stop and not to return any JSON string to the AJAX function that called the script.

As @Victor correctly pointed out in his answer you could disable all errors for the script (or parts of it) by setting error_reporting(0) at the beginning at the script. In that case you wouldn't have to check other possible errors, e.g. if the db credentials in the $_POST array are not set or empty.

As @Ben mentioned in his comments you could also make mysqli throw exceptions to avoid fatal errors by setting mysqli_report(MYSQLI_REPORT_STRICT). This would also be a feasible solution for your problem.

As a general consideration you should make these settings in your php.ini:

display_errors = Off
error_log = syslog

This prevents error message from being exposed to the www (security) while making them available for developers in /var/log/syslog (maintainability).

digijay
  • 1,329
  • 4
  • 15
  • 25
  • @digijay The error control operator disables errors only for given string. The other errors will still be displayed (for example, if `$_POST['db_user']` is not defined). Check my update for a new example. – Victor Dec 25 '18 at 12:04
  • Errors should never pass silently! – Ben Dec 25 '18 at 12:07
  • @Ben but how would you return them as a json message to an ajax call? – digijay Dec 25 '18 at 12:10
  • 1
    @digijay Thank you , i think u are made a best solution – MR. A Dec 25 '18 at 12:13
  • @digijay You can tell mysqli to throw exceptions instead of getting a warning. mysqli_report(MYSQLI_REPORT_STRICT). @ is a bad style in my opinion. Imagine you have to work with this kind of foreign code. If its not working and there is no error reported at all, real pain in the a**. Especially with a function where you easy can pass wrong credentials. – Ben Dec 25 '18 at 19:44