• MySQL知识库 :: installation
  • Why is that when I run a PHP script it says it "Cannot load MySQL extension?"

  • Discussion

    When running a PHP script for the first time on a server using Linux, you may receive the following message:

    Cannot load MySQL extension, please check PHP Configuration.

    This message could be due to a couple of things. It may be that MySQL support was not enabled when PHP was installed. It could be the the php-mysql shared libraries weren't installed. It could be that your php.ini file is configured incorrectly.

    Solution

    One reason why you may be unable to run a PHP script is that when PHP was installed, it wasn't done so with MySQL support enabled. To check your configuration to be sure, you could run a simple script using the phpinfo() function like this:

    <?php
    print phpinfo();
    ?>

    Put this script on your server in a web accessible directory and then open it with your web browser. Search the page for --with-mysql You should see something like the following in the midst of the Configure Command section:

    '--with-mysql=shared,/usr'

    As of PHP 4 --with-mysql is enabled by default. So, it's unlikely that this will be your problem. Another possibility is that your php.ini file isn't configured properly. You can search the file from the command-line and to look for mysql.so like this:

    grep mysql.so /etc/php.ini 
    extension=mysql.so

    This line will probably be found in your PHP configuration file. However, it may be that it's commented out with a pound-sign (#) at the beginning of the line. If so, edit the file and remove the pound-sign. When finished, you will need to restart any scripts or daemons that are running that will need this change. One such daemon is the Apache htppd daemon for web services. Restarting it will reload the PHP module and in so doing apply the change to php.ini. This can be done by entering something like the following from the command-line:

    /etc/init.d/httpd restart

    Another possiblity for being unable to run your PHP script is that your server doesn't have the php-mysql package installed. If you're running Redhat or Fedora Linux, you can enter the following from the command line to download and install it:

    up2date php-mysql

    For other distributions of Linux, you can go to RPMfind.net to download a copy for your particular distribution and version.