{"id":276,"date":"2012-02-10T15:01:54","date_gmt":"2012-02-10T15:01:54","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=276"},"modified":"2022-02-17T07:14:04","modified_gmt":"2022-02-17T07:14:04","slug":"mysql-errors","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/errors-mysql\/mysql-errors","title":{"rendered":"MySQL Errors"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Object Oriented Style<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Turn on error reporting<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Turn on mysqli throwing exceptions by itself (no if() echo $maindb->error required)\n  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Displaying error with -&gt;prepare()  sql statement<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"my sql statement....\");\n  if ($stmt === false)\n    throw new Exception('QUERY ERROR: ' . $maindb->error);<\/code><\/pre>\n\n\n\n<p>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: <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/40719953\/mysqli-prepare-returns-false-but-mysqli-error-is-an-empty-string\" target=\"_blank\">https:\/\/stackoverflow.com\/questions\/40719953\/mysqli-prepare-returns-false-but-mysqli-error-is-an-empty-string<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Procedural\u00a0Style<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Displaying A MySQL Error<\/h5>\n\n\n\n<p>die(mysql_error()) is very handy when debugging why a MySQL function isn&#8217;t working and just failing <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $result = mysqli_query($dblink, \"SELECT * FROM some_table WHERE some_field = 'abc' \") or die(mysqli_error($dblink));<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Show Failed Query Error<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$result = mysqil_query($dblink, \"INSERT INTO my_table (\n\t\t\t\tsome_id,\n\t\t\t\temail_address\n\t\t\t) VALUES (\n\t\t\t\t$some_id,\n\t\t\t\t'$email'\n\t\t\t)\");\n\tif ($result != TRUE)\n\t\techo \"QUERY ERROR: \" . mysqli_error($dblink) . \"&lt;br>\";<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Show Last Error<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\techo \"LAST ERROR: \" . mysqli_error($dblink);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Style Turn on error reporting Displaying error with -&gt;prepare() sql statement 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\u00a0Style Displaying A MySQL Error die(mysql_error()) is very handy when debugging why a MySQL function isn&#8217;t [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-errors-mysql"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":2998,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/276\/revisions\/2998"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}