title: How to email details of all PHP exceptions/errors, including mysqli ones?
body: <p>How can I get all PHP exceptions, both ones generated by mysqli and normal php ones to be sent to me by email?</p>
  
  <p><strong>The problem environment</strong></p>
  
  <p>I am using a shared server at my ISP and don't have access to any admin type PHP configuration files. On the server I have just a hundred or so lines of PHP that performs a query against a MySQL database and if any rows get returned, it executes a few SQL UPDATE commands. This is run every day as a cron job. There is no associated web site (I don't even have a web site on that server). The server hosts a db used for remote connections and the php simply performs some maintenance on it it.</p>
  
  <p>Since there is no 'user' and no 'web page' to give feedback to I want all errors and exceptions emailed to me.</p>
  
  <p><strong>Things I have tried / researched</strong></p>
  
  <p>I found out that </p>
  
  <pre><code>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  </code></pre>
  
  <p>will convert mysqli errors that would normally result simply in FALSE being returned into an exception that will be written to the error log. That helps.</p>
  
  <p>... that I can email text using eg</p>
  
  <pre><code>$email = "my message";
  Error_log($email, 1, "myemailaddress@myserver.com", "From: defaultemail@hostingserver.com");  
  </code></pre>
  
  <p>and that I can get the details of the error by writing my own error handler eg</p>
  
  <pre><code>function the_error_handler($number, $message, $file, $line, $vars) //putting $vars in shows all the variables the server knows about
  {
      $email = " An error ($number) occurred on line $line in $file.  $message ";
      Error_log($email, 1, "myemailaddress@myserver.com", "From: defaultemail@hostingserver.com");
  
      };
  </code></pre>
  
  <p>and setting it using</p>
  
  <pre><code>set_error_handler('the_error_handler'); 
  </code></pre>
  
  <p><strong>minimum, reproducible example</strong></p>
  
  <p>In the code below I have put all this together and deliberately introduced two errors (trying to echo a variable that doesn't exist and trying to execute a query on a table that doesn't exist.</p>
  
  <pre><code>&lt;?php
  
  function the_error_handler($number, $message, $file, $line, $vars) //putting $vars in shows all the variables the server knows about(inluding passwords)
  {
      $email = " An error ($number) occurred on line $line in $file.  $message ";
      Error_log($email, 1, "myemailaddress@myserver.com", "From: defaultemail@hostingserver.com");
  };
  
  set_error_handler('the_error_handler');
  
  echo $NonExsitentVariable;  //&lt;--deliberate error
  
  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  
  $servername = "65.78.165.199";
  $username = "test_user"; //execute, select, update and create temp files rights
  $password = "U7frgh%^yT";
  $dbname = "devlopment_db";
  
  $the_sql = "SELECT  * FROM non_existent_table;";   //&lt;-- another deliberate error
  $conn    = mysqli_connect($servername, $username, $password, $dbname); // Create connection
  $result  = mysqli_query($conn, $the_sql); //run the query and get a pointer to the result
  $rows    = mysqli_fetch_all($result, MYSQLI_ASSOC); //convert to an associative array of rows to send back 
  ?&gt;
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <p>The result is that the non existent variable error gets emailed to me OK but the  mysqli error just gets put into the error log on the server and not emailed.</p>
  
  <p>How can I get both, or all errors, sent to me by email? 
  (Obviously the real code is a bit more complex and would confuse the issue if posted in full here but the principle is the same if I can understand how to deal with this simpler example)</p>
  
  <p><strong>Background</strong></p>
  
  <p>I have been programming and lecturing in computing for around 35 years until retiring a while back but have never had the need (or wish!) to program in PHP. This is one of my first scripts and likely to be my last so if you are able to help please be reasonably explicit in what I need to do as I'm not too familiar with the PHP syntax or programming environment)</p>
  
  <p><strong>Some SO posts I've looked at</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/9643289/with-php-and-mysqli-is-it-possible-to-catch-all-mysqli-errors-in-one-place-acro">With PHP and mysqli, is it possible to catch all mysqli errors in one place across all PHP scripts?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4866649/good-way-to-email-exceptions">Good Way to Email Exceptions</a></p>
  
  <p><a href="https://stackoverflow.com/questions/41424517/php-exceptions-error-messages">PHP exceptions error messages</a></p>
  
  <p><a href="https://stackoverflow.com/questions/25506227/php-exception-inside-catch-how-to-handle-it">PHP exception inside catch: how to handle it?</a></p>