21

I just installed PHP and Apache on my home PC. When I try to call function mysql_connect I get:

fatal error: call to undefined function mysql_connect.

I have loaded php.ini where I have uncommented lines extension=php_mysql.dll and extension=php_mysqli.dll and changed extension directory to extension_dir = "C:\php\ext" - which is the directory where files php_mysql.dll and php_mysqli.dll are. How can I fix this problem?

Output of phpinfo(): http://jsfiddle.net/MMTwA/

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Nikolas Jíša
  • 699
  • 1
  • 10
  • 28
  • 2
    Have you restarted apache after editing the php.ini? :) And do you also have MySQL installed? Maybe you should have a look at XAMPP (http://www.apachefriends.org/en/xampp-windows.html) which is an easy to use package containing apache, php and mysql. – Quasdunk Dec 18 '11 at 11:30
  • Yes I have restarted it. Yes I have MySQL installed though even if I hadn't I would still get "Undefined function...". I'll probably look at that XAMPP if I don't solve this, thanks. – Nikolas Jíša Dec 18 '11 at 11:56
  • Try copying the `libmysql.dll` to `C:\Windows\System32`. And, again, if you just need a development environment, have a look at XAMPP. It comes with everything integrated, has a GUI, works out of the box and will save you a lot of headache (especially under Windows!). – Quasdunk Dec 18 '11 at 12:23
  • I have libmysql.dll in C:\Windows\System32 already, I'll try that XAMPP if I dont solve this by evening, thanks. – Nikolas Jíša Dec 18 '11 at 12:28
  • Providing the contents of might help. – Bryan Dec 18 '11 at 12:32
  • Since I don't know how to upload data to this site ... Here is link for my index.php with phpinfo(): http://mysharegadget.com/file/326277013.1/7c529e9ae1fdc4bef9bf3fb8e7ae8b7b17f042f7/cs – Nikolas Jíša Dec 18 '11 at 12:49
  • @NikolasJíša That link doesn't seem to work. "The connection was reset". – Bryan Dec 18 '11 at 12:53
  • Uhm, that s weird - works fine for me. How about this one http://pomalu1.edisk.cz/stahni/78861/index.php_41.84KB.html – Nikolas Jíša Dec 18 '11 at 13:00
  • @NikolasJíša Where is your php.ini located? – Bryan Dec 18 '11 at 13:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5955/discussion-between-bryan-and-nikolas-jisa) – Bryan Dec 18 '11 at 13:22
  • in C:\php\php.ini... it loads probably, because if I change for example display_errors = On to Off, it really doesnt show errors then... – Nikolas Jíša Dec 18 '11 at 13:49

13 Answers13

15

After looking at your phpinfo() output, it appears the mysql extensions are not being loaded. I suspect you might be editing the wrong php.ini file (there might be multiple copies). Make sure you are editing the php file at C:\php\php.ini (also check to make sure there is no second copy in C:\Windows).

Also, you should check your Apache logs for errors (should be in the \logs\ directory in your Apache install.

If you haven't read the below, I would take a look at the comments section, because it seems like a lot of people experience quirks with setting this up. A few commenters offer solutions they used to get it working.

http://php.net/manual/en/install.windows.extensions.php

Another common solution seems to be to copy libmysql.dll and php_mysql.dll from c:\PHP to C:\Windows\System32.

Bryan
  • 2,167
  • 20
  • 28
  • I am editing the correct php.ini - well when I try for example to change display_errors = On to Off then I don't have error messages... I have checked the apache log and found there lines like "PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysql.dll' - The specified module could not be found.\r\n in Unknown on line 0". I will look at the link you posted, thanks – Nikolas Jíša Dec 18 '11 at 15:28
  • @NikolasJíša I added another possible solution to my answer. Give that a try. (copy files to system32) – Bryan Dec 18 '11 at 15:39
  • Still not working :)... Neverminds thanks for your time and help, I'll try that XAMPP tomorrow. – Nikolas Jíša Dec 18 '11 at 16:16
4

Background about my (similar) problem:

I was asked to fix a PHP project, which made use of short tags. My WAMP server's PHP.ini had short_open_tag = off. In order to run the project for the first time, I modified this setting to short_open_tag = off.

PROBLEM Surfaced: Immediately after this change, all my mysql_connect() calls failed. It threw an error

fatal error: call to undefined function mysql_connect.

Solution: Simply set short_open_tag = off.

Drigs
  • 71
  • 6
3

My PC is running Windows 7 (Apache 2.2 & PHP 5.2.17 & MySQL 5.0.51a), the syntax in the file "httpd.conf" (C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf) was sensitive to slashes. You can check if "php.ini" is read from the right directory. Just type in your browser "localhost/index.php". The code of index.php is the following:

<?php echo phpinfo(); ?>

There is the row (not far from the top) called "Loaded Configuration File". So, if there is nothing added, then the problem could be that your "php.ini" is not read, even you uncommented (extension=php_mysql.dll and extension=php_mysqli.dll). So, in order to make it work I did the following step. I needed to change from

PHPIniDir 'c:\PHP\'

to

PHPIniDir 'c:\PHP'

Pay the attention that the last slash disturbed everything!

Now the row "Loaded Configuration File" gets "C:\PHP\php.ini" after refreshing "localhost/index.php" (before I restarted Apache2.2) as well as mysql block is there. MySQL and PHP are working together!

Darius Miliauskas
  • 3,283
  • 4
  • 30
  • 50
2

Check your php.ini, I'm using Apache2.2 + php 5.3. and I had the same problem and after modify the php.ini in order to set the libraries directory of PHP, it worked correctly. The problem is the default extension_dir configuration value.

The default (and WRONG) value for my work enviroment is

; extension_dir="ext"

without any full path and commented with a semicolon.

There are two solution that worked fine for me.

1.- Including this line at php.ini file

extension_dir="X:/[PathToYourPHPDirectory]/ext

Where X: is your drive letter instalation (normally C: or D: )

2.- You can try to simply uncomment, deleting semicolon. Include the next line at php.ini file

extension_dir="ext"

Both ways worked fine for me but choose yours. Don't forget restart Apache before try again.

I hope this help you.

jagumon
  • 21
  • 4
2

You have probably forgotten to restart apache/wamp/xamp/whatever webserver you use, you need to do that in order to make it work

Martin.
  • 10,271
  • 3
  • 40
  • 67
1

Hi I got this error because I left out the ampersand (&) in

; php.ini
error_reporting = E_ALL & ~E_DEPRECATED
zzapper
  • 4,455
  • 5
  • 47
  • 44
1

Be sure you edited php.ini in /php folder, I lost all day to detect error and finally I found I edited php.ini in wrong location.

nobjta_9x_tq
  • 1,107
  • 13
  • 16
0

One time I had a problem while using Off instead of off. And also check the pads one more time... The path has to be exact. Also add the following line to your environmental variable.

C:\your-apache-path\bin; C:\your-php-path\bin;C:\your-mysql-path\bin

If you are in Windows, right click My Computer, select properties, and navigate to the Advanced tab... (is Windows 7). Click on Advanced system settings first then select the Advanced tab and then Environmental variables. Select PATH and click on Edit. Make a copy of the string in a .txt file for back up (it might be empty)--- set your environmental variables... Log out and log back in.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
0

After change our php.ini, make sure to restart Apache web server.

user2618844
  • 342
  • 3
  • 12
0

Just for future reference, copying all these extension files to Windows/System or Windows/System32 is unnecessary.

All that is required is a copy of the php.ini file you edit in the PHP dir to copied to the root Windows dir.

phpinfo will clearly explain the below: Configuration File (php.ini) Path C:\Windows

Logical sense will explain that php wants to load a config located in the Windows dir. :-)

WolF-X
  • 1
0

Since mysql_connect 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. by default xampp does not load it automatically

in your php.ini file you should uncomment

;; extension=php_mysql.dll

to

extension=php_mysql.dll

Then restart your apache you should be fine

Pascal
  • 2,280
  • 3
  • 22
  • 39
0

This same problem drove me nuts (Windows 10, Apache 2.4, MySql 5.7). For some reason (likely PATH related), the .dlls would not load after uncommenting the correct exension dlls and extension_dir = "ext" in php.ini. After trying numerous things, I simply changed "ext" to use the full directory path. For example. extension_dir = "c:/phpinstall/ext" and it worked.

mark
  • 1
-1

I think that you should use mysqli_connect instead of mysql_connect