{"id":1500,"date":"2022-08-30T15:17:03","date_gmt":"2022-08-30T15:17:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/23\/fatal-error-uncaught-exception-mysqli_sql_exception-with-message-no-index-used-in-query-prepared-statement-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:03","modified_gmt":"2022-08-30T15:17:03","slug":"fatal-error-uncaught-exception-mysqli_sql_exception-with-message-no-index-used-in-query-prepared-statement-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/fatal-error-uncaught-exception-mysqli_sql_exception-with-message-no-index-used-in-query-prepared-statement-collection-of-common-programming-errors\/","title":{"rendered":"Fatal error: Uncaught exception &#39;mysqli_sql_exception&#39; with message &#39;No index used in query\/prepared statement&#39;-Collection of common programming errors"},"content":{"rendered":"<p>When I run the following code, I get the error saying<\/p>\n<blockquote>\n<p>Fatal error: Uncaught exception &#8216;mysqli_sql_exception&#8217; with message &#8216;No index used in query\/prepared statement&#8217;<\/p>\n<\/blockquote>\n<pre><code>$mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database');\n        if (mysqli_connect_errno()) {\n            printf(\"DB error: %s\", mysqli_connect_error());\n            exit();\n        }\n\n    $get_emp_list = $mysql-&gt;prepare(\"SELECT id, name FROM calc\");\n    if(!$get_emp_list){\n        echo \"prepare failed\\n\";\n        echo \"error: \", $mysql-&gt;error, \"\\n\";\n        return;\n    }\n    $get_emp_list-&gt;execute();\n    $get_emp_list-&gt;bind_result($id, $emp_list);\n<\/code><\/pre>\n<p>And this is the able schema &#8212;<\/p>\n<pre><code>--\n-- Table structure for table `calc`\n--\n\nCREATE TABLE IF NOT EXISTS `calc` (\n  `id` int(12) NOT NULL,\n  `yr` year(4) NOT NULL,\n  `mnth` varchar(12) NOT NULL,\n  `name` varchar(256) NOT NULL,\n  `paidleave` int(12) NOT NULL,\n  `balanceleave` int(12) NOT NULL,\n  `unpaidleave` int(12) NOT NULL,\n  `basesalary` int(12) NOT NULL,\n  `deductions` int(12) NOT NULL,\n  `tds` int(12) NOT NULL,\n  `pf` int(12) NOT NULL,\n  `finalsalary` int(12) NOT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=MyISAM DEFAULT CHARSET=latin1;\n<\/code><\/pre>\n<ol>\n<li>\n<p>Take a look at this bug-report : <strong><code>#35450<\/code><\/strong> mysqli extension reports too many warnings<\/p>\n<p>Quoting a few sentences of a note :<\/p>\n<blockquote>\n<p>Mysqli extension throws too many warnings.<br \/>\nFor example, &#8220;SELECT * FROM table&#8221; results in a warning: &#8220;Warning: mysqli::query(): No index used in query\/prepared statement SELECT * FROM table &#8230;&#8221;<\/p>\n<\/blockquote>\n<p>And, quoting another note, which seems interesting :<\/p>\n<blockquote>\n<p>Use <code>mysqli_report()<\/code> to disable that.<\/p>\n<\/blockquote>\n<p>Humph, unfortunately, that function is deprecated&#8230;<\/p>\n<\/li>\n<li>\n<p>The fatal error is not in MySQL; the missing index notification is a relatively low-severity warning.<\/p>\n<p>The <em>fatal<\/em> error is in your PHP code, because of the following three conditions:<\/p>\n<ul>\n<li>mysqli reports a <em>lot<\/em> of warnings, even for relatively harmless conditions.<\/li>\n<li>You&#8217;re throwing <code>mysqli_sql_exception<\/code> for all errors <em>and<\/em> warnings due to your <code>mysqli_report(MYSQLI_REPORT_ALL);<\/code> line.<\/li>\n<li>Your PHP code is not catching that exception (i.e. it&#8217;s not in a <code>try{}<\/code> block with an appropriate <code>catch(){}<\/code> block), and uncaught exceptions are fatal.<\/li>\n<\/ul>\n<p>You can&#8217;t do much about the first one, as mentioned in the other answer. So, you can fix it either by changing your <code>mysqli_report(...)<\/code> setting to <code>MYSQLI_REPORT_STRICT<\/code> or <code>MYSQLI_REPORT_OFF<\/code> (or indeed anything other than <code>MYSQLI_REPORT_ALL<\/code>), or in combination with this, by properly by using <code>try{}<\/code> and <code>catch(){}<\/code> appropriately within your code.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-23 19:34:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When I run the following code, I get the error saying Fatal error: Uncaught exception &#8216;mysqli_sql_exception&#8217; with message &#8216;No index used in query\/prepared statement&#8217; $mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die(&#8216;There was a problem connecting to the database&#8217;); if (mysqli_connect_errno()) { printf(&#8220;DB error: %s&#8221;, mysqli_connect_error()); exit(); } $get_emp_list = $mysql-&gt;prepare(&#8220;SELECT id, name FROM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1500","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1500"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1500\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}