Click to See Complete Forum and Search --> : IS there a library I'm missing? "Undefined function mysql_connect"


adp
10-23-2008, 04:19 PM
This may belong in Newbies, or Database - but I think it belongs here:
I'm a member of a long list of folks getting a fatal error when trying to connect to mysql using PHP. I'm running PHP 5.2.6 and MySQL server version 5.0.67 on a Macbook running OS X Leopard - 10.5.5.

Fatal error: Call to undefined function mysql_connect() in /opt/local/apache2/htdocs/install/save.php on line 348

No problem connecting to the server through the terminal, just through PHP scripts, so I'm assuming now that the error message is literally true - PHP can't find or doesn't know what to do with the function "mysql_connect".

Where is PHP supposed to find functions that address MySQL? Is it in a library? Maybe there's something I can do to "connect" PHP with a library containing the function? Something to do with "dependencies" maybe? I'm concerned that this may not be the only PHP - SQL function that is "undefined" and others will show up once I do get connected, but I don't know how to find out.

A search using GREP for "mysql_connect" in all non-binary files in /opt/ where the Macports installation put files turned up only a few responses. One was php.ini, but only the listing in mysql.h (which opened in Xcode - version 3.1.1) appeared to define it as a function. The other listings were just using the function - as in save.php above where the error appeared.

I also found a reference to the mysql_connect being "deprecated" in favour of mysql_real_connect. I tried that in a test file, but it also failed with a similar message.

I'm running out of ideas - can anyone suggest another direction to go??

NogDog
10-23-2008, 07:00 PM
The MySQL extension is not part of the default PHP5 installation. See http://www.php.net/manual/en/mysql.installation.php for more info on the libraries you need and such.

Better yet, just use the newer MySQLi extension (http://www.php.net/manual/en/book.mysqli.php), instead.

adp
10-24-2008, 12:07 PM
Thanks for the response, NogDog.

It looks like both mysql and mysqli extensions were contained in the Macports PHP5 port, although I'm not clear if or how they were installed.
I added a default socket location to the php.ini file, but attempts to link to MySQL with either mysql_connect or mysqli_real_connect or mysql_real_connect all failed in my simple test file. Still able to connect using the terminal, though.

The PHP installation guide gives specifics for Windows users ("Windows users may enable the extension php_mysql.dll inside php.ini.") - looks easy to hook up the needed library files - but for Unix users there's a caveat ("Unix users, at least the ones who know what they are doing, tend to always build PHP against their system's libmyqlclient library simply by adding the --with-mysql=/usr option when building PHP."). I don't qualify :)
I've read on other forums that there can be conflicts unless previous versions are removed. I can "clear" the Macports installation, but can I control where a new install will go so that it works with the other Macports files already in place? Although I'm prepared to take risks, my knowledge of PHP and Unix is weak, so I don't know what to try. Probably should have titled this post "Senior citizen rookie needs backup!"

bradmasterx
10-27-2008, 05:05 AM
mysqli_connect should work, check your php.ini

adp
10-27-2008, 10:41 AM
Thanks for the suggestions, folks.
I decided to uninstall the Macports php5 port and try again. This time I added some variants during the installation, but now have a new problem.
Since this is now an "install php" problem, I'll post to the Install forum and see what comes up there.
Thanks again for the help.