how to catch mysql error in php

Solutions on MaxInterview for how to catch mysql error in php by the best coders in the world

showing results for - "how to catch mysql error in php"
Jakob
16 Mar 2020
1<?php
2define("MYSQL_CONN_ERROR", "Unable to connect to database.");
3
4// Ensure reporting is setup correctly
5mysqli_report(MYSQLI_REPORT_STRICT);
6
7// Connect function for database access
8function connect($usr,$pw,$db,$host) {
9   try {
10      $mysqli = new mysqli($host,$usr,$pw,$db);
11      $connected = true;
12   } catch (mysqli_sql_exception $e) {
13      throw $e;
14   }
15}
16
17try {
18  connect('username','password','database','host');
19  echo 'Connected to database';
20} catch (Exception $e) {
21  echo $e->errorMessage();
22}
23?>