{"id":3246,"date":"2022-02-19T14:14:22","date_gmt":"2022-02-19T14:14:22","guid":{"rendered":"https:\/\/www.dpscomputing.com\/blog\/?p=3246"},"modified":"2022-02-19T14:14:23","modified_gmt":"2022-02-19T14:14:23","slug":"fatal-error-class-mysqli-not-found-on-line","status":"publish","type":"post","link":"https:\/\/www.dpscomputing.com\/blog\/2022\/02\/19\/fatal-error-class-mysqli-not-found-on-line\/","title":{"rendered":"Fatal error: Class &#8216;MySQLi&#8217; not found on line"},"content":{"rendered":"\n<p>A common error that occurs, particularly for newcomers, to SQL development is a fatal error when trying to refer to the MySQLi class such as:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"155\" height=\"129\" data-attachment-id=\"3248\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2022\/02\/19\/fatal-error-class-mysqli-not-found-on-line\/php-mysqli\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg\" data-orig-size=\"155,129\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;David Smith&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1645279945&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"php-mysqli\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg\" alt=\"\" class=\"wp-image-3248\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg 155w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli-150x125.jpg 150w\" sizes=\"auto, (max-width: 155px) 100vw, 155px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>Fatal error: Class 'MySQLi' not found (&#91;URL]) on line &#91;X]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-issue\">The Issue<\/h2>\n\n\n\n<p>The issue being caused here is that the class, in this case &#8216;MySQLi&#8217; cannot be initialised.  Forget for a moment that this is a MySQLi issue and consider that it is actually an issue that you can encounter with any object-orientated programming development.  The fundamental issue here is that you&#8217;re trying to load a class that isn&#8217;t or cannot be found.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"initialising-the-mysqli-class\">Initialising the MySQLi Class<\/h2>\n\n\n\n<p>You will likely be using a simple bit of code to initialise your MySQLi class.  Something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$mysqli = new MySQLi($db_server, $db_user, $db_password, $db_name);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>$mysqli &#8211; this is the object where you want the reference to the new MySQLi instance to be stored.  <\/li><li>$db_server &#8211; the address to your database server.  <\/li><li>$db_user &#8211; the username for use with your database (server-wide or database-specific).  <\/li><li>$db_password &#8211; the password for the database user stored in $db_user<\/li><li>$db_name &#8211; the database table that you want to access.  <\/li><\/ul>\n\n\n\n<p>It&#8217;s always worth double-checking both the structure of your initialisation code to ensure there are no typos or missing parameters as this is a common cause.  Also double-check all the details you are providing in your parameters.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check-your-mysqli-installation\">Check your MySQLi installation<\/h2>\n\n\n\n<p>A common reason for the above error occurring is simply forgetting to install MySQLi.  This can easily be done locally during development by going to the PHP website for <a rel=\"noreferrer noopener\" href=\"https:\/\/www.php.net\/manual\/en\/mysqli.installation.php\" target=\"_blank\">MySQLi installation<\/a>.  On Windows machines you will likely find that you don&#8217;t (or haven&#8217;t) had to do this manually if you&#8217;ve used the binary installer.  The MySQLi extension is enabled by default for PHP versions 5.3.0 onwards.  <\/p>\n\n\n\n<p>If you have used the binary installer and it&#8217;s still not working, trying to reinstall PHP to see if it resolves the issue as there could have been a failed installation or configuration that didn&#8217;t complete correctly.  <\/p>\n\n\n\n<p>If you are running your code on AWS, you can use a command such as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum install php71-mysqli<\/code><\/pre>\n\n\n\n<p>This will trigger the installation of MySQLi.  <\/p>\n\n\n\n<p>To install the native driver on Linux you would need something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php-mysqlnd<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"things-to-double-check-when-initialising-the-mysqli-class\">Things to double check when initialising the MySQLi Class<\/h2>\n\n\n\n<p>If you&#8217;re still having trouble instantiating the MySQLi class after checking the installation you can try the following common resolutions to issues:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You have included a well structure call to the MySQLi constructor<\/li><li>The values for your parameters\/variables have been initialised prior to the call<\/li><li>The values passed to the MySQLi constructor are correct.  <\/li><li>The database user specified exists and has privileges for the table you have specified &#8211; either global access or a database specific user.  <\/li><li>The database table you are passing in exists.  <\/li><\/ul>\n\n\n\n<p>Hope that helps!  If you have any more tips or issues, drop them in the comments below!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common error that occurs, particularly for newcomers, to SQL development is a fatal error when trying to refer to the MySQLi class such as: The Issue The issue being caused here is that&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3248,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2139],"tags":[2857,2858,2856,2860,2859,2863,2861,2862,2864],"class_list":["post-3246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development-issues","tag-class-mysqli-not-found","tag-class-mysqli-not-found-on-line","tag-fatal-error-class-mysqli-not-found","tag-mysqli","tag-mysqli-not-found","tag-new-mysqli","tag-php-mysqli","tag-php-mysqli-issue","tag-sql-development"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2022\/02\/php-mysqli.jpg","jetpack_shortlink":"https:\/\/wp.me\/p3nsfA-Qm","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/comments?post=3246"}],"version-history":[{"count":1,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3246\/revisions"}],"predecessor-version":[{"id":3249,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3246\/revisions\/3249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media\/3248"}],"wp:attachment":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media?parent=3246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/categories?post=3246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/tags?post=3246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}