MySQL Forums
Forum List  »  PHP

Re: Call to undefined function mysqli_connect()
Posted by: Ulf Wendel
Date: June 12, 2006 09:50AM

sdfv sdfv wrote:
> "Call to undefined function mysqli_connect()".

All,

can we try to create a FAQ style answer for this which we can put on http://forge.mysql.com/wiki/Main_Page ? Here's my draft:

Windows: Call to undefined function mysqli_connect()

In 99,99% of all cases misconfiguration is the cause of the PHP error message “Call to undefined function mysql_connect()”. Configuration issues can be tricky to debug and solve, because there are so many different ways to do the configuration in a correct or in a wrong way. Due to the variety of possible misconfiguration scenarios there is no simple “three-steps-to-success”-guide that covers all cases. But don't worry, it is easy to learn how to configure PHP.

Background
Technically speaking the PHP interpreter consists of a small language core and many extensions. The language core implements only some 500 out of the more than 3,000 functions available in PHP (http://www.zend.com/phpfunc/statistics.php). Some 2,500 functions are provided through extensions. To use these functions, you have to enable the appropriate extensions and load them into the PHP interpreter.

The function mysqli_connect() is part of the MySQL Improved extension, mysqli. If the mysqli extension is not loaded into PHP, you will get the error message “Call to undefined function mysqli_connect()” once you invoke the function mysqli_connect(). On Windows, PHP extensions are extra library files with an “.dll” filename suffix, e.g. php_mysqli.dll.
Where to get the required library files
Generally speaking we recommend that Windows users use the Connector/PHP downloads provided on http://dev.mysql.com/downloads/connector/php/. The main advantage of this distribution is that it is build for the latest versions of PHP and MySQL. The binaries from php.net ship with an old version of the MySQL Client library, which is partly incompatible with the MySQL Server 5.0.

Back to the configuration!
How to load an extension into PHP
The PHP configuration file has the name php.ini. The php.ini defines which extensions get loaded into PHP. The list of extensions to be loaded into PHP is specified using extension directives. The location where PHP looks for the correspondending library files is set by the extension_dir directive.

The php.ini configuration file can reside in many directories and there can be more than one php.ini file on your system. For example, different installation procedures can have created multiple php.ini files. And it could be that there is no php.ini at all so far.
Where is the php.ini configuration file?
Luckily, PHP can tell you which php.ini file it is using, if any. Create a simple test.php file with the following content. The function phpinfo() output a lot of PHP information among others it lists the php.ini file used and the extensions loaded.

<?php phpinfo(); ?>

Invoke the test.php script in the same way as you will be using PHP later on for your programs. If you are setting up a web server with PHP, invoke the script through the web server, e.g. by opening 127.0.0.1/test.php in the browser. If you plan to use PHP on the CLI, invoke PHP on the CLI. This is an important step, because the way you run PHP influences the location where PHP searches for the php.ini. The php.ini will be searched in the following locations:

SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath (Windows Registry location)
The PHPRC environment variable
Current working directory (for CLI)
The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows)
Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option
Check the output of the phpinfo() function for “Configuration file (php.ini) Path”. This is the php.ini file that will be used by PHP. If there is no entry in this column, it means that you a.) have no php.ini file or it can not be found by PHP in any of the above listed locations. In case a php.ini exists, but is not recognized, consult the PHP manual for details of your environment (Apache, IIS, CLI, ...)! Not all details are described here. Don't forget to check the user notes in the PHP manual. Some of them are a great help to work around common pitfalls.

If you do not have a php.ini at all, go to the PHP installation directory (e.g. C:\php) and rename the php.ini-recommended file to php.ini.

If you are using a PHP web server module, the php.ini is read when the web server gets started. If you are using a CLI or CGI version of PHP, the configuration file is read for every invocation of PHP. This is important to know, because it depends on your setup when changes to the php.ini will become effective. For the web server module, you have to restart the web server to make PHP recognize any changes made to the php.ini since the last start of the web server. This is not needed for the CLI or CGI version of PHP. Recall this, when you make changes to the php.ini – this includes relocating the php.ini configuration file!
Loading the MySQLi extension into PHP
The directives extension and extension_dir tell PHP which extension to load and where the extension library files can be found. Typically, the directory “extensions” in the PHP installation directory (e.g. C:\php\extensions) gets used as the extension library.

Open the php.ini file shown in the output of phpinfo() for editing. Modify the extension_dir directive to point to the extension directory you want to use:

// The location where PHP searches for
extension_dir = C:\php\extensions

This line makes PHP search for the extension libraries in the directory C:\php\extensions. Which extensions will be loaded is defined with the extension directive. You can have multiple extension directives like the one below which loads the mysqli extension into PHP:

// MySQLi extension
extension=php_mysqli.dll

Save the modified php.ini configuration file. Check the output of phpinfo() again. Recall that a restart of the web server is required if you run PHP as a web server module before the new php.ini settings become effective.

Check the output of phpinfo() for a section “mysqli”. If it does, you managed to install the MySQLi extension which has been designed for use with the MySQL Server version 4.1.3 and newer. Check the row “Client API version”. If the version is the same as the version of the MySQL Server you want to connect to, you are done: you have successfully loaded the MySQLi extension into PHP and your system is using the correct library version..

Installing the proper MySQL Client library (libmysql.dll)
If the version shown is not the same as the version of your MySQL Server, for example because you used the php.net downloads which are delivered with version 4.1.7 of the MySQL Client library (libmysql.dll), but you want to connect to the MySQL Server 5.0, then you have to update the MySQL Client library, see http://dev.mysql.com/downloads/connector/php/ for notes on incompatible versions.

The MySQL Client library is a file called libmysql.dll. The library is required by the MySQLi extension to talk to the MySQL Server. During the start up of PHP, the MySQLi extension tries to load this library. The library libmysql.dll is usually located in the installation directory of PHP, e.g. C:\php\libmysql.dll . However, like with the php.ini there can be none or multiple copies of the file on your system:

the PHP installation directory
the Windows library directory (e.g. C:\Windows\System32)
the bin/ directory of a local MySQL Server installation

If you are getting an error message like “PHP Startup: Unable to load dynamic library 'c:/php/ext/php_mysql.dll'”, then the MySQL Client library was not found at all. If you have not downloaded the Connector/PHP from http://dev.mysql.com/downloads/connector/php/ , do it now.

Extract the libmysql.dll from it an put it in the PHP installation directory. Make sure that it really gets used. As said at the beginning, there can be many reasons for misconfiguration. Regarding the libmysql.dll look at the places where PHP looks for the library...

Good luck!

Ulf

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.