Categories: FAQ | PHP

PHP FAQ


Contents

[edit] Windows: "Call to undefined function mysqli_connect()"

[edit] Summary

The PHP extensions for mysql and mysqli functions are not enabled by default in PHP 5. Also, the MySQL client libraries are not included with PHP 5. You need to perform some configuration to enable these sets of functions.

See http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.php5 for PHP's reason why MySQL functions are not enabled by default.

In 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.

[edit] 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.

[edit] 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 4.1 and later.

Back to the configuration!

[edit] 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.

[edit] Where is the php.ini configuration file?

There can be more than one php.ini file on your computer. Web servers are configured to use one of them. You need to query PHP via the web server to find out the location of the php.ini file the web server is using. This section explains how to do that.

The PHP function phpinfo() lists details about your PHP installation, including the php.ini file being used, the directory from which PHP extensions are loaded, and all PHP extensions loaded. Create a simple test.php file with the following content:

<?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 http://127.0.0.1/test.php in the browser. If you plan to use PHP through the command line interface (CLI), invoke PHP on the CLI. This is an important step, because the way you run PHP influences the location where PHP searches for php.ini. Windows will search for php.ini in the following locations:

Check the output of phpinfo() for "Configuration file (php.ini) Path". This is the php.ini file which PHP will use. If there is no such entry, either that you have no php.ini file, or PHP cannot find in any of the location listed above.

If you have put a php.ini file somewhere but PHP cannot find it, check the PHP manual for where PHP expects to find it in your PHP environment (Apache, IIS, CLI, or whatever). The FAQ does not describe all details. Also 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, php.ini is read when the web server starts. If you are using a CLI or CGI version of PHP, the configuration file is read on every invocation of PHP. This is important to know, because it affects when changes to the php.ini take effect. For the web server module, you must restart the web server so PHP can recognize changes made to php.ini since the last time the web server started. This is not necessary for the CLI or CGI version of PHP. Remember that this includes php.ini relocations!

To reiterate:

[edit] 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..

[edit] Installing the proper MySQL Client library

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:

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 and 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.

[edit] PHP4: "Client does not support authentication protocol"

When attempting to connect a PHP4 application to a MySQL 4.1 or later server, you will probably see this error message, which is pretty confusing.

[edit] The Cause

MySQL 4.1 changed its default password encryption method. So MySQL 4.0 or earlier clients cannot authenticate (that is, log in) against MySQL 4.1 or later servers. See http://dev.mysql.com/doc/refman/5.0/en/old-client.html for more information on this.

PHP4 comes with an old MySQL client statically linked in. You cannot upgrade it without recompiling PHP from source. This is inconvenient, and if you are using PHP on a hosting provider, you probably don't have privilege to do this.

PHP.net has no plans to release an updated distribution of PHP4 with a newer MySQL client. This issue has been reported several times on bugs.php.net, but they always close the issue with a status of "Bogus", meaning it's not a PHP problem.

[edit] The Solution

There are two solutions to this problem:

1. You can upgrade to PHP5, and install a MySQL 4.1 or later client library. See http://forge.mysql.com/wiki/PHP_FAQ#Windows:_.22Call_to_undefined_function_mysqli_connect.28.29.22 for complete instructions on performing that installation.

2. Alternatively, you can enable the "old-passwords" option on the MySQL Server. The options file is typically "my.ini" or "my.cnf" and is located in the MySQL Server data directory. Make sure there is a line in the file like this:

old-passwords

NOTE: if you change the server to use old-passwords, any existing accounts need to have their password changed to the old password format. See http://dev.mysql.com/doc/refman/5.0/en/old-client.html for an example, or the following:

mysql> SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('mypassword'); 

Where 'username'@'hostname' is the account and client host you use, and 'mypassword' is the password you want to use for that account.

Retrieved from "http://forge.mysql.com/wiki/PHP_FAQ"

This page has been accessed 53,880 times. This page was last modified 04:24, 10 October 2006.

Find

Browse
MySQLForge
Main Page
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Special pages
New pages
File list
Statistics
Bug reports
More...