# Solved: PHP's MySQL functions not working; I think php.ini isn't being found/configured right



## adsmith82 (Jul 19, 2006)

Initially, I was using IIS 5.1 with PHP 5.2.5 and MySQL Server 5.0 when I got an error using the mysql_connect function. Here is the error:

Fatal error: Call to undefined function mysql_connect() in C:\[path]\[file].php on line 2

And here is the code:

<?php
$con = mysql_connect("localhost","[username]","[password]");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
?>

When I test the PHP server with the phpinfo function, it works just fine. However, the only mentions of "mysql" in the page produced by phpinfo is PATH locations. I thought phpinfo was supposed to show all the extensions configured in the php.ini file.

While trying to get it to work, I changed some things and somehow broke the PHP part of it and never figured out how to fix that, so I just installed Apache 2.2.8. Well PHP is back but I still cannot use mysql functions. In my php.ini file, I have the following lines:

extension_dir="C:\Program Files\PHP\ext"
...
[PHP_MYSQL]
extension=php_mysql.dll

The php_mysql.dll file, as well as many others, does exist at that location. My httpd.conf file includes (all at the end of the file):

LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
AddType application/x-httpd-php .php .php5

# configure the path to php.ini
PHPIniDir "C:/Program Files/PHP"

I have to comment out the PHPIniDir directive to get Apache to start without crashing. When I moved my PHP folder to C:\PHP and made all necessary changes in php.ini and httpd.conf, I could leave the PHPIniDir uncommented and Apache would start. Originally I thought it may have been crashing because of the space in "Program Files", but I copied the php.ini to C:\WINDOWS, made the changes in the config files, and it crashed again. C:\Program Files\PHP and C:\WINDOWS are both in the PATH system variable and C:\PHP is not, so I think that is why it did not crash when PHPIniDir points to C:\PHP; it could not find the php.ini file. This also makes me believe that the problem is somewhere in my php.ini file, because it crashes when it finds the file.

I read somewhere that at the top of the phpinfo() page, the location for "Configuration File (php.ini) Path" indicates whether the php.ini was actually found or not. I read that if the location says C:\WINDOWS or otherwise ends in a folder name instead of a file name (does not end in \php.ini), then the php.ini was not found and is not being used. This makes sense since my PHPIniDir directive is commented out and the MySQL extension and many other extensions are not listed on the phpinfo() page.

Am I right in that the problem is probably somewhere in my php.ini file? And what should I look for to find the problem?


----------



## tomdkat (May 6, 2006)

First, post the error you get when you try to connect to the database using Apache as the web server. Second, look in the Apache error log to see what other information might be contained in there regarding the connection problem. Third, in your extension_dir directive, do you need to specify forward slashes instead of backslashes?

backslash version --> extension_dir="C:\Program Files\PHP\ext"
forward slash version --> extension_dir="C:/Program Files/PHP/ext"

Also, since you have things stored in folders with embedded spaces in the names, you might need to "escape" the space, like this:

extension_dir="C:/Program\ Files/PHP/ext"

Peace...


----------



## adsmith82 (Jul 19, 2006)

i posted the error message... 
Fatal error: Call to undefined function mysql_connect() in C:\[path]\[file].php on line 2

and php config file uses \, not /... mysql uses /

and i don't think the php config file needs escape characters. besides, i had the same problem when pointing PHPIniDir to C:\WINDOWS


----------



## tomdkat (May 6, 2006)

adsmith82 said:


> i posted the error message...
> Fatal error: Call to undefined function mysql_connect() in C:\[path]\[file].php on line 2


Above, you indicated that was the error you got with *IIS*, not with Apache. Are you receiving the same error message when you run with Apache?



> and php config file uses \, not /... mysql uses /


Apache uses forward slashes as well. 

Now, I see where you mention php_mysql.dll above but I don't see any mention of libmysql.dll. Have you read the following from the PHP MySQL page?



> MySQL is no longer enabled by default, so the php_mysql.dll DLL must be enabled inside of php.ini. Also, PHP needs access to the MySQL client library. A file named libmysql.dll is included in the Windows PHP distribution and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH. See the FAQ titled "How do I add my PHP directory to the PATH on Windows" for information on how to do this. Although copying libmysql.dll to the Windows system directory also works (because the system directory is by default in the system's PATH), it's not recommended.


Is libmysql.dll where it needs to be? Also, did the Windows installer for PHP install in Program Files by default or did you specify that as an installation location?

Peace...


----------



## adsmith82 (Jul 19, 2006)

yes, same error with apache, I think PHP is giving the error

i think i meant apache uses /, not mysql

libmysql.dll is in my PHP folder already, but is not mentioned in php.ini, i'm not sure if it needs to be

if i recall correctly, default folder was in Program Files


----------



## tomdkat (May 6, 2006)

Based on the info I quoted above, the libmysql.dll file needs to be in the Windows system path. Is the location of that DLL in your Windows system path? Not the user path but the system path? The error you're getting is PHP complaining it can't find the MySQL APIs and those will be defined in libmysql.dll. Are you running on Windows XP or Vista or some other version?

Peace...


----------



## adsmith82 (Jul 19, 2006)

xp pro sp2... php put C:\Program Files\PHP at the beginning of my system path at installation


----------



## adsmith82 (Jul 19, 2006)

i think the system path is ok, because when apache's PHPIniDir directive pointed to a directory that was not in the system path but contained php.ini, the server started, but when PHPIniDir pointed to either C:\WINDOWS or C:\Program Files\PHP, both of which are in the system path and at the time both contained the php.ini file, apache would not start the server. When apache can find the files it will not start, so I just comment out the PHPIniDir and the server starts and phpinfo() works but I don't think it is using php.ini for configuration


----------



## adsmith82 (Jul 19, 2006)

well, i uninstalled php and reinstalled it, this time only base installation + mysql extension + manual... nothing else (i installed all extensions the first time)... and the php installation added itself to the end of httpd.conf for apache which I don't remember it doing before, but it seemed to add the same things that I put in manually earlier

so phpinfo() now reports the mysql extension being enabled and the connection to my database works, but I'm not sure what exactly was causing the problem... it may have just been the fact that I installed all 108 or however many php extensions there are, but there may have been something else that I did differently; i can't be sure without more testing, but it is working now

and just for the record and for future reference, the installation processes for the newest versions of apache, mysql, and php seem to be all set up to not require any manual configuration to get the basic php/mysql/webserver going... but I am not sure which order they should be installed in, probably apache first, then the other two in either order

anyway, i appreciate your help


----------



## tomdkat (May 6, 2006)

adsmith82 said:


> so phpinfo() now reports the mysql extension being enabled and the connection to my database works, but I'm not sure what exactly was causing the problem...


You never answered my question above.  The path to libmysql.dll needed to be in the Windows system path. Now that you've re-installed, where is libmysql.dll located on your system and is *that* location in your system path?

Peace...


----------



## adsmith82 (Jul 19, 2006)

adsmith82 said:


> libmysql.dll is in my PHP folder





adsmith82 said:


> php put C:\Program Files\PHP at the beginning of my system path at installation


so that wasn't the problem


----------



## tomdkat (May 6, 2006)

I conducted a test of my own today. I downloaded PHP 5.2.5 from the main distribution website and installed it on my Windows XP system. During the install, I installed ALL extensions, just like you did the first time. Since I didn't have a web server to use with it, I opted not to have it setup a web server. After the install, I ran the "php" executable to invoke the CLI to see what would happen. I got a TON of errors about missing DLLS, etc. One commonly occuring one was OCI.DLL which is to connect to an Oracle database. Of course, I don't have Oracle installed so I don't have that DLL. Maybe when you installed PHP the first time, it configured support for stuff you didn't have installed and that was getting in the way of your MySQL use. When you re-installed and didn't install the other stuff, it didn't try to load all that other stuffand then things started working. Just a guess. 

Peace...


----------



## adsmith82 (Jul 19, 2006)

that's very possible, thanks again for your efforts to help me out


----------

