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);
UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.