title: PHP MySQLi connection and query error handling. How?
body: <p>The following PHP code works. I just don't seem to be able to handle its errors in a custom way.</p>
  
  <p>For example, when I intentionally misspell anything in the connection string to return code "3" for database down, my AJAX script just hangs in beforeSend forever...</p>
  
  <p>This is what I got:</p>
  
  <pre><code>&lt;?php  
  
    if(isset($_POST["postT_VAL"])) {
  
    $client_id    = $_POST["postCLIENT_ID"];
    $project_id   = $_POST["postPROJECT_ID"];
    $mainsheet_id = $_POST["postMAINSHEET_ID"];
    $field_name = $_POST["postT_ID"];
    $field_value = $_POST["postT_VAL"];
  
    $link = mysqli_connect("database.domain.com", "username1", "password1", "db220474");
  
    if (!$link) {
  
    /* return 3 = database offline */
    echo "3";
  
    } else {
  
    /* build query */
    $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  
  
    /* execute query */    
    mysqli_query($link, $sql);
  
    /* return 0 = no update / 1 = successful update */
    echo "".mysqli_affected_rows($link);
  
    /* close connection */
    mysqli_close($link);
  
   }
  
   }
  
  ?&gt;
  </code></pre>
  
  <p><strong>New Research</strong></p>
  
  <p>Alright. After some research I have found that this works. It appears that you need to tell mysqli to throw exceptions.. For some reason this is different than just trying to handle an error the "IF" way. Any suggestions for improvement? </p>
  
  <pre><code>mysqli_report(MYSQLI_REPORT_STRICT);
  
        try {
             $link = mysqli_connect("database.domain.com", "username1", "password1", "db220474");
        } catch (Exception $e ) {
             echo "3";
             exit;
        }
  </code></pre>
  
  <p><strong>Code Update</strong></p>
  
  <p><strong>Here's is the final tested and working PHP solution for all to see.</strong></p>
  
  <pre><code>&lt;?php  
  
    /* Status Codes
  
       return 0 = Nothing to Update
       return 1 = Successful Update Query
       return 2 = Database Connection refused
       return 3 = MySQL Query Error OR Wrong URL Parameters */
  
    mysqli_report(MYSQLI_REPORT_OFF);
  
    if(isset($_GET["postT_VAL"])) {
  
    $client_id    = $_GET["postCLIENT_ID"];
    $project_id   = $_GET["postPROJECT_ID"];
    $mainsheet_id = $_GET["postMAINSHEET_ID"];
    $field_name = $_GET["postT_ID"];
    $field_value = $_GET["postT_VAL"];
  
    try {
         $link = mysqli_connect("domain", "username", "password", "database");
    } catch (Exception $e) {
         // echo "".$e-&gt;getCode();
         /* return 2 = Database Connection refused */
         echo "2";
  
         exit;
    }
  
    /* Build dynamic Update Query string */
    $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  
  
    /* Execute Update Query */    
    if(!mysqli_query($link, $sql)) {
  
    echo "3";
    /* Close Connection */
    mysqli_close($link);
  
    exit;
  
    } else {
  
    /* return 0 = Nothing to Update / 1 = Successful Update Query */
    echo "".mysqli_affected_rows($link);
  
    /* Close Connection */
    mysqli_close($link);
  
    }
  
   }
  
  ?&gt;
  </code></pre>