Skip to content

How to catch mysql error in PHP?

define("MYSQL_CONN_ERROR", "Unable to connect to database.");

// Ensure reporting is setup correctly

// Connect function for database access
function connect($usr,$pw,$db,$host) {
   try {
      $mysqli = new mysqli($host,$usr,$pw,$db);
      $connected = true;
   } catch (mysqli_sql_exception $e) {
      throw $e;

try {
  echo 'Connected to database';
} catch (Exception $e) {
  echo $e->errorMessage();
See also  Python code snippet - How to sort subset of rows in pandas df?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.