php: mysql_connect() failing with no error message

2019-08-13 12:15发布

问题:

I am running a simple test script from the command line on CentOS 5.6 with the PHP package installed from PHP 5.3 on CentOS/RHEL 5.6.

PHP runs fine in all other cases but when I hit mysql_connect() it fails without error.

If I run

$ php -m

I am not seeing MySQL as an installed module.

However I have added extension=mysql.so to my php.ini and rebooted.

Output of 'rpm -qa | grep php'

php-common-5.3.10-1.w5
php-5.3.10-1.w5
php-cli-5.3.10-1.w5

Output of 'yum install php-mysql'

  --> Missing Dependency: php-common = 5.1.6-27.el5_7.5 is needed by package php-mysql-5.1.6-27.el5_7.5.x86_64 (updates)
php-pdo-5.1.6-27.el5_7.5.x86_64 from updates has depsolving problems
  --> Missing Dependency: php-common = 5.1.6-27.el5_7.5 is needed by package php-pdo-5.1.6-27.el5_7.5.x86_64 (updates)
Error: Missing Dependency: php-common = 5.1.6-27.el5_7.5 is needed by package php-pdo-5.1.6-27.el5_7.5.x86_64 (updates)
Error: Missing Dependency: php-common = 5.1.6-27.el5_7.5 is needed by package php-mysql-5.1.6-27.el5_7.5.x86_64 (updates)
 You could try using --skip-broken to work around the problem
 You could try running: package-cleanup --problems
                        package-cleanup --dupes
                        rpm -Va --nofiles --nodigest
The program package-cleanup is found in the yum-utils package.

回答1:

 I am not seeing MySQL as an installed module.

Did you install it?

# yum install php-mysql

(from the same repo you installed php from).

EDIT:

run this:

yum --enablerepo=webtatic install php-mysql

this tells yum to get the packages from webtatic repository (in addition to system configured repositories). If you want webtatic among system enabled repositories, run:

 yum --enablerepo=webtatic install  webtatic-release


回答2:

First, some advice: stop using mysql libraries and use PDO or at least the mysqli libraries.

Now for some troubleshooting help: the first step is to be 100% sure the mysql is loading. Create a script (web based) with:

phpinfo();

Confirm that somewhere on this page there is a block indicating that the mysql extension is being loaded.

Wherever you are testing an are uncertain of errors, right before you call mysql_connect add these lines:

ini_set('display_errors', 1);
ini_set('log_errors', 1); 
error_reporting(E_ALL | E_STRICT);

This will ensure you aren't suppressing the errors somewhere else before the connect. Some frameworks sometimes do this.

If it's indeed the module is loading and with the added error logging code you still don't get a valid error. I'd try connecting in a command line php script. Look in the php.ini file to see what "error_log" is set to. This is the default log it will it will log to--sometime http based settings override logging, so even if errors are thown, you don't see or can't find them. A command line php script factors this out.

Last but not least, you can always post your connection string to this post. Try connecting from the command line on the server as your script using the mysql command with the same credentials as your mysql_connect. You might be able to connecting just fine, but have bad credentials.

If the module is not loading, try to find exactly where the module resides and put the full path into the php.ini. Yum sometimes installs stuff not where php.ini is looking for it. At the end of the Yum install, it sometimes tells you the path, if not, to find it with brute force, do this:

cd /
find . -name 'mysql.so'

so in the php.ini, instead of

extension=mysql.so

put

extension=/path/to/found/mysql.so

Good luck!