Object Oriented Style

Turn on error reporting
  //Turn on mysqli throwing exceptions by itself (no if() echo $maindb->error required)
  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Displaying error with ->prepare() sql statement
  $stmt = $maindb->prepare("my sql statement....");
  if ($stmt === false)
    throw new Exception('QUERY ERROR: ' . $maindb->error);

There is an issue with this in that you need to first use $stmt=False; if using $stmt again, otherwise $maindb->error will be empty. Explanation here: https://stackoverflow.com/questions/40719953/mysqli-prepare-returns-false-but-mysqli-error-is-an-empty-string

Procedural Style

Displaying A MySQL Error

die(mysql_error()) is very handy when debugging why a MySQL function isn’t working and just failing

  $result = mysqli_query($dblink, "SELECT * FROM some_table WHERE some_field = 'abc' ") or die(mysqli_error($dblink));
Show Failed Query Error
	$result = mysqil_query($dblink, "INSERT INTO my_table (
				some_id,
				email_address
			) VALUES (
				$some_id,
				'$email'
			)");
	if ($result != TRUE)
		echo "QUERY ERROR: " . mysqli_error($dblink) . "<br>";
Show Last Error
	echo "LAST ERROR: " . mysqli_error($dblink);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

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