PHP Fatal error: Call to undefined function: mysql_connect()
I was aware that mysql_connect has been discontinued in C API, but why am I getting this error? I found that mysql_connect has been deprecated in favor of mysql_real_connect(). While that doesn't solve our issue at hand, I do want to take a moment to point out that Ingo Tomahogh has a good tip posted on http://dev.mysql.com about migrating your code from using mysql_connect() to mysql_real_connect function. To put it in Ingo's words:
If you need to compile old programs still using this function, you might want to add the following macro definition to your programs (possibly via the compiler's command line) so you needn't change all calls to mysql_connect() :
#define mysql_connect(m,h,u,p) mysql_real_connect((m),(h),(u),(p),NULL,0,NULL,0)
You should also make sure the MYSQL pointer is never NULL in these calls, though.
Thanks to David Phillips for pointing out the following
In response to the mysql_connect() macro, also make sure to call mysql_init() before using it. You didn't need to (or at least it worked) with older versions of the library. You will immediately crash if you don't.
Another thing worth noting here is that the errors and return values for mysql_real_connect() are the same as mysql_connect(). More information on mysql_connect function in available from dev.mysql.com.
Now, back to troubleshooting our "Call to undefined function: mysql_connect()". Some other MySQL error messages:
PHP Warning: mysql_connect(): Client does not support authentication protocol requested by server; consider upgrading MySQL client
The issue PHP is complaining about here is threefold.
1. missing mysql.so extension definition in /etc/php.ini, and/or
2. missing mysql.so file on system
3. missing lib*mysql.so* files on your system
First, get a copy of mysql.so and put it in /usr/lib/php4 or equivalent.
cp -p /old/usr/lib/php4/mysql.so /usr/lib/php4/
Now, put the following line in /etc/php.ini
After restarting httpd (Apache), I got
PHP Warning: Unknown(): Unable to load dynamic library '/usr/lib/php4/mysql.so' - libmysqlclient.so.10: cannot open shared object file: No such file or directory in Unknown on line 0
To fix this, get a copy of libmysqlclient.so.10. Assuming you have got a copy on your hard drive
cp -p /old/usr/lib/libmysqlclient.so.10 /usr/lib/
Now, restart Apache, and voila! you should be able to connect. To verify, load phpinfo() and you should see a section similar to the following:
|Active Persistent Links||0|
|Client API version||HIDDEN|
Related MySQL troubleshooting tips from fellow webmasters online.
Resolving a Fatal error: Call to undefined function mysql_connect() in RedHat
PHP works, but not with mysql
could not connect mysql5 from php5
Call to undefined function: mysql_connect()
MySQL on Technorati
MySQL on Del.icio.us