Solution - Unable to load dynamic library C php ext php_mysql.dll - the specified module could not be found

Solution : Unable to load dynamic library C:\php\ext\php_mysql.dll - the specified module could not be found

Environment

  • OS : Windows XP
  • PHP Version : 5.0.4
  • Apache Version : Apache 2
  • MySQL Version : MySQL 4.1
  • PHP Installed at C:\php

Problem

On a new installation of PHP, after having installed Apache and MySQL when I enable the php_mysql.dll module in the php.ini file, upon restarting Apache I get the error message "Unable to load dynamic library C:\php\ext\php_mysql.dll - the specified module could not be found." twice. Initially I was getting the error "Unable to load dynamic library .\php_mysql.dll - the specified module could not be found." I am able to load other modules, just not php_mysql.dll.

Solution

The confusing thing about this error message is that "the specified module" that could not be found, is not php_mysql.dll as one would think. I'll see the error message that it can't find the module C:\php\ext\php_mysql.dll and then go to that directory and find myself looking at that exact file. The module that isn't being found is a dll called libmysql.dll. This library is located in a couple places, C:\php\libmysql.dll and C:\Program Files\MySQL\MySQL Server 4.1\bin\libmySQL.dll. The one thing I did that fixed this problem was to copy libmysql.dll from C:\php to C:\Windows\System32. I've confirmed that the instance of libmysql.dll that php is looking for is "C:\php\libmysql.dll". This effectively moves the libmysql.dll into the path (in that c:\windows\system32\ is always in the path). You can also modify your global environement path, and then restart your computer.

Solution User Comments

The C:\php\libmysql.dll is the one that should be used. It is all a matter of setting the "Path" environment variable to also point to C:\php. It should work from there. This is documented in the manual - it's just not easy to spot.
Comment by Rayne

After setting the Path-Environment Variable you should restart your system!
Comment by sofastar

Rather than copy libmysql.dll to Windows\System32, copying it to Apache's root folder (typically Program Files\Apache Group\Apache2) should also work. This may be preferable to burying in it System32 with a zillion other dlls
Comment by anonymous

It seems that the php_mbstring.dll is mandantory for loading php_mysql.dll (beside the mentioning for php_exif.dll below). I just wanted to point that out, because it nearly drove me crazy.
Comment by Borgond

PHP 5.0.xx also looks at the php.ini file for extensions' paths. While it is true that many errors are the result of failing to find libmysql.dll, it might be that the error is telling the truth!
Comment by anonymous

Assuming a standard installation, the following will be true:

  1. PHP is installed in c:\php.
  2. PHP.ini is located in c:\windows
  3. c:\php is not in the path variable.
  4. extension dlls are in c:\php\ext

Steps to make php work:

  1. Add c:\php to the system path.
    (control panel->system->advanced->env variables->path)
  2. REBOOT!
  3. open php.ini and uncomment the appropriate extension lines

    (i.e. extension=php_mysql.dll)

  4. modify php.ini to look for extensions in the path directory

    (i.e. extension_dir = "C:\php\ext\")

  5. restart the webserver.

It has been my experience that most users forget step 4. The only way that they get the dlls to work is to copy them to the system32 directory, which is a really bad practice.

Addendum

The main difficulty in troubleshooting this issue is two-fold. One, the issue crops up in the default installation of PHP. This means that anyone who installs Apache PHP and MySQL on a Windows system, will have to manually copy this dll file to get it to work. This is absurd considering that the PHP Apache MySQL combination is about as vanilla as you get. Two, the error message that is displayed is an outright lie. This error message, "Unable to load dynamic library ./php_whatever.dll - the specified module could not be found." is displayed when any php extension can't be loaded. Another example is when you attempt to enable the php extension php_exif.dll. The exact same error message pops up, when the actual cause of the failure is that php_mbstring.dll must be loaded in the php.ini file prior to php_exif.dll. Again, the default behaviour up to PHP 5.0.4 is for the extensions to be listed alphabetically in the php.ini instead of by dependency.

So in summary, if you see the error "Unable to load dynamic library ./php_whatever.dll - the specified module could not be found." ignore it, consider it to read "Unable to load dynamic library ./php_whatever.dll - something prevented PHP from loading the dynamic library but I can't tell you what."

What next

I think there are three initiatives that need to be implemented by the PHP developers in order to make PHP usable by non techies.

  • Make the default installation of PHP on any platform work right out of the box. The idea that dll files must be manually copied into the C:\Windows\System32 folder is asinine. The idea that one would have to go into the php-dict.ini or php-recommened.ini and re-order the extensions so they load in the order that the dependencies require is absurd.
  • Provide meaningful information in error messages and if there is no meaningful information, don't provide false information. I believe that it would be easy to change an error message that says "Unable to load dynamic library ./php_mysql.dll - the specified module could not be found." to "Unable to load dynamic library ./php_mysql.dll - the libmysql.dll module could not be found." At least don't show any error message if the contents are not true. "Unable to load dynamic library ./php_exif.dll - the specified module could not be found." should be changed to something like "Unable to load dynamic library .\php_exif.dll - the specified module requires that php_mbstring.dll be loaded."
  • Re-write the installation documentation from scratch. The online installation instructions (the instructions one must follow to install PHP, especially on Windows) were developed progressively, creating a veritable Choose-Your-Own-Adventure install process. There is no deliniation between what text in the page is a step in the process that must be completed and what is informational. There are many many different pages which all must be read to catch all of the steps, of which no clear order is provided to implement them.

Other Gotchas

  1. If your troubleshooting PHP under windows, and making modifications to the php.ini file and getting nowhere, make sure that the php.ini that you're modifying is actually getting loaded. Create a test php file that displays phpinfo() and check the "Configuration File (php.ini) Path" value. Make sure it's pointed to the file you're modifying. The reason being, if the php can't find a php.ini where it expects to find it, it doesn't complain. It just uses all the defaults. I had to move php.ini into C:\WINDOWS. I believe one could also set the path to include whatever directory php.ini is in and it would work. You would however have to reboot the computer to get it to take the new path.
  2. If you're setting the path in Windows to either get php to find php.ini or get some extension to find it's related dlls, remember you have to reboot the whole computer after setting the path for it to become real.
  3. php_curl.dll requires libeay32.dll, and ssleay32.dll be available. This means somewhere in the path. If you haven't modified the path, you're going to need to copy these files to c:\Windows\System32
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Apr 30, 2009

    Anonymous says:

    Hi everyone, I would just like to add my two cents of experience regarding this...

    Hi everyone,

    I would just like to add my two cents of experience regarding this matter.

    I have PHP (5.2.9) running under apache server (2.2.11) on WINXP and also have been hit hard by this "new error" which I never encountered using PHP 4.

    Since I am very skeptic about moving anything into system32 dir on windows and/or modifying PATH variable I used the following solution:

    1. as suggested before on this page use php.ini which resides in PHP installation directory (and is picked up by apache properly) and modify

    extention_dir = "<your php ext dir>" and uncomment

    extention = php_mysql.dll and

    extention = php_mysqli.dll

    2. (and this is as far as I am concerned the most important step) I changed settings to how apache handles PHP.

    In Apache conf/httpd.conf file I have made a reference to PHP configuration file which I use separately (I believe this is Apache 2 feature):

    Include conf/php5.conf

    Of course dont forget:

    AddHandler application/x-httpd-php .php
    AddHandler application/x-httpd-php-source .phps

    Then I created separatephp5.conf file in the same directory (apache's /conf/ dir) and put following lines inside:

    #load the php main library to avoid dll hell
    Loadfile "<your PHP root>\php5ts.dll"
    Loadfile "<your PHP root>\libmysql.dll"
    # INIT PHP with Apache
    LoadModule php5_module "<your PHP root>\php5apache2_2.dll"
    #set the php.ini location so that you don't have to waste time guessing where it is
    PHPIniDir "<your PATH to PHP.INI file yust DIR!!!>"

    like PHPIniDir "c:\php"

    Hope this helps someone spend couple of hours less to set things right.

    Cheers

    JaniP (Slovenia)

    1. May 15, 2010

      Anonymous says:

      I had the same problem... Inluding the following additional lines to my httpd.c...

      I had the same problem...

      Inluding the following additional lines to my httpd.conf (as stated in the first comment) solved the problem for me:

      #load the php main library to avoid dll hell
      Loadfile "<your PHP root>\php5ts.dll"
      Loadfile "<your PHP root>\libmysql.dll"

      For some reason, even after putting libmysql.dll in my PATH and rebooting, php_mysql.dll could still not find it and thus did not load.

      Now everything works as expected

      PS: I cant remember having such trouble when installing PHP4 years ago...

    2. Jun 16, 2011

      Anonymous says:

      Wow THANK YOU SO MUCH for this perfect solution. Sadly, I'd already wasted HOURS...

      Wow THANK YOU SO MUCH for this perfect solution. Sadly, I'd already wasted HOURS on the install before I came across this and tried it. Five minutes later I started up, no problem. Now if I can just stop myself from choking whomever is responsible for their crummy installation...

  2. Apr 30, 2009

    Anonymous says:

    The solution commnted is the best..... If you have the same prolem in postgres...

    The solution commnted is the best.....

    If you have the same prolem in postgresql change this lines:

    Loadfile "<your PHP root>\libmysql.dll"     -->  Loadfile "<your PHP root>\libpq.dll"

    extention = php_mysql.dll                        -->  extention = php_pgsql.dll

    It works for me!!!! I hope you dont have more problems with this.

  3. May 13, 2009

    Anonymous says:

    I gave up on all of this and used something called Vertrigo which is a ready to ...

    I gave up on all of this and used something called Vertrigo which is a ready to go WAMP stack.

    I wouldn't recommend it for anything major, but then again, I wouldn't recommend PHP on Windows for anything major.

    Personally, I've left PHP entirely for Django/Python and have left Windows entirely for Mac - Linux still for the server stuff. I highly recommend people follow on that path.

    I'm only working on this as a final consulting project...

  4. Aug 04, 2009

    Anonymous says:

    Just wanted to confirm that copying the file libmysql.dll from C:\PHP to C:\Wind...

    Just wanted to confirm that copying the file libmysql.dll from C:\PHP to C:\Windows\System32 resolved my issue of not loading php_mysql.dll.

    Thanks a lot and really appreciate for this.

    Akshay

  5. Nov 24, 2009

    Anonymous says:

    I copied libmysql.dll to system32 folder and its working fine....Thanks Tanweer...

    I copied libmysql.dll to system32 folder and its working fine....Thanks

    Tanweer Ahmad

  6. Jun 09, 2010

    Anonymous says:

    Thank you i worked for my - using loadfile in httpd.conf - good work after spend...

    Thank you i worked for my - using loadfile in httpd.conf - good work after spending more than 2 hrs looking to find an answer that worked for me.  cheers

  7. Apr 18, 2011

    Anonymous says:

    Which php and my SQL version should we use?

    Which php and my SQL version should we use?

  8. Jan 08, 2012

    Anonymous says:

    Omg, seriously. You are my hero. I've spent hours on debugging this.

    Omg, seriously. You are my hero. I've spent hours on debugging this.