Unable to connect to mysql using php. Fatal error: Call to undefined function mysqli_

Hi guys,
I am new to php and am learning it from “Build Your Own database driven web site using Php and MySql”.

I get the following error when i run the below code.
$link = mysqli_connect(‘’, ‘root’, ‘mypassword’);

Fatal error: Call to undefined function mysqli_connect() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2

I am using apache http server 2.2 and php 5.4.11 and operating system is Windows 7. (I have installed php and mysql separately and not used Wamp server). I am able to connect to mysql from command prompt.

I have read enormous number of threads related to the same error(some on this very site). Still am unable to successfully run the code.
I have checked the following points:
File - php.ini
i) doc_root = C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs
ii) extension_dir = “C:\php\ext”
iii) extension=php_mysqli.dll (uncommented)

In the file - C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf i have added
LoadModule php5_module “C:/PHP/php5apache2_2.dll”
AddType application/x-httpd-php .php
PHPIniDir “C:/PHP”

Also i have learned that php 5.3+ does not need extra setup configurations with mysql due to Mysql native driver(mysqlnd). Yet however i have added the path of directory containing libmysql.dll and the directory containing php.ini in the System Path and restarted the computer.

I have also run the command phpinfo(INFO_MODULES) and I find mysqlnd enabled but there is no mention of mysqli. I have also confirmed that i am editing the correct php.ini file. What am I actually missing?

Any help/ guidance appreciated :slight_smile:

Create a script to echo out the phpinfo

<?php echo phpinfo() ?>

I’m going to hazard a guess that mysqli hasn’t been compiled into your PHP installation.

Also, you’d be better served to learn how to access the database using the PDO library, unless your needing to learn mysqli to deal with legacy systems.

Firstly, well asked question! It’s nice to see someone who’s clearly looked around and tried all the common fixes then come here and said what they’d tried.

Run Michael’s script and check the top for “Configuration File (php.ini) Path” and make sure it is reading the one from c:\php\php.ini

Hi Michael,
Thanks for such a quick reply :slight_smile: . I didn’t expect such a quick response from a forum.
However I am not sure I have understood what you have mentioned.
I had extracted a zip file downloaded from the site http://php.net/downloads.php to folder c:\php and I have the file C:\php\ext\php_mysqli.dll present. Were you referring to this file when you meant mysqli hasn’t been compiled into the PHP installation?

Anyways i will be exploring the PDO library which you have mentioned. I am finding getting a start point into PHP very tough (with some many frameworks and content management systems). Hence I had decided to go with whatever is mentioned in the book.

It sounds like you are putting your dev environment together from scratch. I’d recommend using XAMPP while learning. It has almost any module you’re likely to encounter in a tutorial already setup, and if you run into trouble its general architecture is familiar to a wide swath of people so it will be easier to find help if you do hit a config error. For the most part a XAMPP installation on your desktop is self contained.

Keep in mind it isn’t that secure out of the box - it wasn’t designed to be. It’s designed for experimentation and skill development, not production.

XAMPP has mysqli inbuilt and has MySQL included out of the box, further it comes with phpMyAdmin which is useful for setting up tables.

Thanks a lot will install XAMPP then.

Truthfully i also wanted to learn how the actual setup works in the deployment environment but i think i better learn using XAMPP first :slight_smile:

While XAMPP is an awesome rapid deployment option, I’d recommend, if at all possible, to make your dev environment as close to your prod environment. For me, this means compiling apache and php from scratch, and saving the ./configure arguments for both. You can painfully find out that running a WAMP server (windows apache mysql php) can be very different from a LAMP server (linux).

EDIT: For a small deployment though, you should be fine. I love setting up a virtual box (https://www.virtualbox.org/) with a LAMP server for development.