Answers for "php catch sql error"

PHP
0

how to catch mysql error in php

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

// Ensure reporting is setup correctly
mysqli_report(MYSQLI_REPORT_STRICT);

// 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 {
  connect('username','password','database','host');
  echo 'Connected to database';
} catch (Exception $e) {
  echo $e->errorMessage();
}
?>
Posted by: Guest on April-09-2021
0

php catch exception

<?php
function inverse($x) {
    if (!$x) {
       throw new Exception('Division durch Null.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
}

// Ausführung fortsetzen
echo "Hallo Welt\n";
?>
Posted by: Guest on August-03-2020

Browse Popular Code Answers by Language