Sunday, October 30, 2005

MySQL5 - PHP4 - Troubleshooting - Call to undefined function: mysql_connect()

Earlier, I showed you how to install MySQL 5 (MySQL5) on a Red Hat Enterprise Linux server. The MySQl 5 installation went just fine for us, however today when I tried to run a test script incorporating a MySQL 5 client to MySQL 4 server connection, I received the following errors

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

extension=mysql.so


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:

mysql

MySQL Supportenabled
Active Persistent Links 0
Active Links 0
Client API version HIDDEN
MYSQL_MODULE_TYPE external
MYSQL_SOCKET /var/lib/mysql/mysql.sock
MYSQL_INCLUDE -I/usr/include/mysql
MYSQL_LIBS -L/usr/lib/mysql -lmysqlclient



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()
on Technorati
on Del.icio.us

4 comments:

Frank said...

Hello lsosheriff,
Yes, it seems you are on the right track. Try putting the mysql.so in your directory and that should resolve the problem. If not, let me know by posting here.

Thanks
Frank

Ryan Lovelett said...

You wouldn't know how to do this on a Windows installation would you?

I tried extrapolating what you've done here to Windows. What happens is that the when I load php_info() the page just keeps loading indefinately. If I uncomment the line and retry. Goes right to the php_info().

Just thought I'd ask.

A said...

Hy,
thank's for help ...but
i follow the all steps but with old lib
the system say
symbol errno, version GLIBC_2.0 not defined in file libc.so.6
with link time reference

so i try to del and make a new
link with new lib... the same error...
mysql run
php 4.4 don't give error
apache 2.0 don't start only if
i put # on auth_mysql.conf ...
sorry I'm lameeeeeee

any suggestions?
thanks anyway ;)))

A said...

Hy,
thank's for help ...but
i follow the all steps but with old lib
the system say
symbol errno, version GLIBC_2.0 not defined in file libc.so.6
with link time reference

so i try to del and make a new
link with new lib... the same error...
mysql run
php 4.4 don't give error
apache 2.0 don't start only if
i put # on auth_mysql.conf ...
sorry I'm lameeeeeee

any suggestions?
thanks anyway ;)))