21

I have MySQL (5.5.23 Community Server), Apache (2.2), and PHP (5.3.13) running on a Fedora 15 machine (64-bit). Each works with no problem on their own.

I thought that PHP was configured/compiled to work with MySQL, but when I try to load any PHP web page which makes a MySQL call then I get the above noted fatal error (in the Apache error log). PHP works okay on its own though, through Apache and at the command line.

When I run php -i from the command line then sure enough it shows all the MySQL bits and pieces, but when I load a page displaying phpinfo() then there are no MySQL modules mentioned.

I've also tried un-commenting "extension=mysql.so" in php.ini, but PHP then complains with "PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysql.so'." (even though the module does reside there). But then I'm not entirely certain whether that line is required to be un-commented or not. [correction: the error stated was actually due to a typo, but running from the command line shows that the module is already loaded anyway - there is a warning from PHP that says so.]

I've also disabled SELinux; stopped/restarted Apache numerous times; yummed MySQL-Devel, in case it made any difference; and, it seems, every other possible thing.

If I run a little PHP test script from the command line, to access the MySQL database, then it works with no problem, so PHP does know about MySQL, but when it runs under Apache it seems to have no clue that MySQL even exists.

I've been trying on and off for months to solve this problem, and seem to have tried everything, but nothing seems to work.

I don't mind having to re-install PHP if I really have to, if somebody can demonstrate the proper way to configure/compile with MySQL support, so that I can get it to work under Apache, although ideally I'd simply prefer to get the existing installation working.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Cheddar
  • 375
  • 1
  • 3
  • 10

7 Answers7

63

In case anyone else faces this, it's a case of PHP not having access to the mysql client libraries. Having a MySQL server on the system is not the correct fix. Fix for ubuntu (and PHP 5):

sudo apt-get install php5-mysql

After installing the client, the webserver should be restarted. In case you're using apache, the following should work:

sudo service apache2 restart
andreimarinescu
  • 3,541
  • 2
  • 25
  • 32
18

Keep in mind that as of PHP 5.5.0 the mysql_connect() function is deprecated, and it is completely removed in PHP 7

More info can be found on the php documentation:

Quote:

Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
* mysqli_connect()
* PDO::__construct()

Tim Penner
  • 3,551
  • 21
  • 36
3

I had this same problem and had to refer to the php manual which told me the mysql and mysqli extensions require libmysql.dll to load. I searched for it under C:\windows\system32 (windows 7) and could not find, so I downloaded it here and placed it in my C:\windows\system32. I restarted Apache and everything worked fine. Took me 3 days to figure out, hope it helps.

Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
0

How about

sudo yum install php-mysql

or

sudo apt-get install php5-mysql
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rahim Khoja
  • 695
  • 13
  • 26
-1

The Apache module PHP version might for some odd reason not be picking up the php.ini file as the CLI version I'd suggest having a good look at:

  • Any differences in the .ini files that differ between php -i and phpinfo() via a web page*
  • If there are no differences then to look at the permissions of mysql.so and the .ini files but I think that Apache parses these as the root user

To be really clear here, don't go searching for php.ini files on the file system, have a look at what PHP says that it's looking at

James C
  • 14,047
  • 1
  • 34
  • 43
  • I'll get my Sherlock hat on and check out what differences there may be, and get back here again, but, as for permissions, then I kind of thought the same as you: Apache will parse as root anyway. The modules all have root owner/group, and are all set at 755. – Cheddar Mar 11 '13 at 20:22
  • It won't stop this from working but the modules don't need to have the exec bit set so 644 is fine. – James C Mar 11 '13 at 20:24
  • Okay, good to know. Well, I'm already a bit confused by trying to compare the output. With both php -i, and phpinfo(), the config file is given as /etc/php.ini, which is the one I've been playing with, and is the only one I am aware of. However, with php -i, the compilation info' shows it compiled with mysql, and other related modules (e.g. pdo, etc), but phpinfo() shows it compiled without (... '--without-mysql' '--without-gd' '--disable-dom' '--disable-dba' '--without-unixODBC' '--disable-pdo' ... etc.); FYI there is only one instance of MySQL and Apache on the server. – Cheddar Mar 11 '13 at 20:56
  • Anyone shed any light? – Cheddar Mar 12 '13 at 16:05
  • In the php.ini file can you try including the mysql.so extension with an absolute path – James C Mar 12 '13 at 19:40
  • Yeah ... just done that, at your suggestion, but still the same (after restarting Apache): extension=/usr/lib64/php/modules/mysql.so. Also did mysqli.so, and mcrypt.so. – Cheddar Mar 12 '13 at 20:27
  • It's definitely the right pathname, and from the command line PHP warns that those modules are already loaded. Really doing my head in ... I just cannot for the life of me think why it doesn't work. – Cheddar Mar 12 '13 at 20:28
  • How are both PHP installs there? Via RPM? – James C Mar 12 '13 at 21:14
  • Well, there's only one PHP install on the machine, which would have been via yum. – Cheddar Mar 12 '13 at 21:31
  • If you can see two different sets of compile options via `phpinfo()` and `php -i` there are two different installs of PHP on the machine – James C Mar 13 '13 at 08:26
  • But each reports as using /etc/php.ini for config, and I can find only one instance of php when I do a "whereis php": php: /usr/bin/php /etc/php.d /etc/php.ini /usr/lib64/php /usr/share/php /usr/share/man/man1/php.1.gz. (i.e. with php being in /usr/bin/, which is where I'd have expected it right? .. although I'd be happy to be proven wrong, in order to solve this. Thx for your time to date! – Cheddar Mar 13 '13 at 12:32
  • However .... I have discovered a difference in one parameter, between php -i and phpinfo(), and it is the "Scan this dir for additional .ini files" parameter. With php -i the value is "/etc/php.d", which exists, and is where the ,ini files for the modules are defined (so that's correct). However, with phpinfo(), even though the main config file is shown as correct (/etc/php.ini) the path to scan for additional .ini files is "/etc/php-zts.d" - which doesn't exist, (thoughI do have them defined as extensions in php.ini anyway). But, no idea where it's getting "php-zts.d" from; obviously wrong. – Cheddar Mar 13 '13 at 15:13
-3

I had received a similar error message. Think I inadvertently typed "9o" at start of first line of php.ini file. Once I removed that, I no longer received the "fatal error" messages. Hope this helps.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Gordon
  • 1
-4

The mysql deamon should be running.

If not try this:

#/etc/init.d/mysql start

Or this:

#service mysqld start

And if you want to add mysql on boot:

# chkconfig --add mysqld
# chkconfig -- level 235 mysqld on

If yes, and it is still not working try this:

Uncomment the following lines in /etc/php/php.ini

extension=mysqli.so
extension=mysql.so

And please check your post above '/usr/lib64/php/modules/msql.so'. It should be mysql.so (if it's mistyped ignore it...)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pixeltracer
  • 60
  • 1
  • 9
  • Yes, already tried that. Restarted MySQL and Apache, just in case. Thanks anyway though. – Cheddar Mar 11 '13 at 19:52
  • 6
    restarting the mysql server won't alter what goes on with the client libraries – James C Mar 11 '13 at 20:09
  • 1
    That's a poor approach. Focus your energies on places that it might make a difference! – James C Mar 11 '13 at 20:25
  • Yes, pixeltracer, a typo. – Cheddar Mar 11 '13 at 20:25
  • Well, having tried everything I can think of I'm out of ideas ... If there's even a remote chance that restarting MySQL may have made a difference then, to me, it was worth trying. – Cheddar Mar 11 '13 at 20:26
  • #pixeltracer - Actually, I realize now that originally it was a typo, but it's subsequently negated now anyway, because (as now mentioned in the original post) at least when running from the command line PHP warns that the module is already loaded. However, I still have the main issue of PHP/MySQL not working together with Apache. – Cheddar Mar 11 '13 at 22:05
  • can you post the exact error message here so we can see whats going on? – pixeltracer Mar 11 '13 at 22:48
  • ive deleted my last comment due to have missed your first line info regarding the LAMPS version you are using. Anyway if you have any change to show us your php.ini and the exact error that is show in pastebin. so we can figure it out. – pixeltracer Mar 11 '13 at 23:10
  • Well, the error is per the title of the original post "PHP Fatal error: Call to undefined function mysql_connect()", which is as reported in the Apache error log. Note also my previous comment showing that while php -i says that PHP is compiled with MySQL support, phpinfo() says that it is not. – Cheddar Mar 11 '13 at 23:29