Summary of solutions to the problem that php cannot connect to mysql

Source: Internet
Author: User

Question 1

Today I installed mysql and php on this book, everything is installed normally (php@IIS7.5-fastCGI ). However, the error "#2002 unable to connect" occurs when you use phpmyadmin to connect to the test. The same problem occurs with other programs.
Check that the mysql extension of php is correctly installed, and the mysql service is started normally-that's strange. And connected with navicat .. It seems that there is a problem with the communication between php and mysql.
Baidu has a problem:
The mysql_connect () function database server (server) parameter is currently set to localhost, but can be accessed again after it is changed to 127.0.0.1.
The system hosts file does not provide resolution from 127.0.0.1 to localhost.

Solution(Take Windows 7 as an example): find the C: \ Windows \ System32 \ drivers \ etc \ hosts file. The default value is read-only. Copy the file to another path, such as the desktop, open the file (open mode: notepad) and add:

127.0.0.1 localhost

If the preceding "#" already exists, remove it.

Question 2


Php scripts can run normally, such as phpinfo ().

If the "mysql_connect" function appears, no response is returned and no error is returned.

For example, $ conn = mysql_connect ("127.0.0.1", "root", "xxxx") or die ("MYSQL database connection failed:". mysql_error ());

Error. log in the apache log Directory

PHP Warning: PHP Startup: Unable to load dynamic library 'C: \ php \ ext \ php_mysql.dll'

Solution

Copy "libmysql. dll" to the bin directory of apache. Restart and observe error. log. If the error disappears, congratulations.

In addition, "libmysql. dll" and "php_mysql.dll" are copied to windows/system32.


Question 3

 

The code is as follows: Copy code
<? // The above code is omitted
$ Link_id = @ mysql_connect ("localhost", "sampadm", "secret ");
// The following code is omitted:
?>

The following prompt is displayed during running:
Fatal error: call to undefined function mysql_connect () in var/www/html/mytest. php

Solution

1. php. ini

Extension_dir is set to "d: \ apache2 \ php \ ext" absolute path. Otherwise, errors may occur in some environments.

Enable the following two functions

Extension = php_mysql.dll
Extension = php_mysqli.dll
 
2. copy libmysql. dll (under the php directory) and php_mysql.dll (under the php/ext directory) to the c: \ windows \ system32 directory (libmysql. the dll must be in the php Directory, for example, libmysql in the directory D: \ apache2 \ php. dll, php_mysql.dll in D: \ apache2 \ php \ ext)
 
3. Test,
1> open the command line first
A) choose "start"> "mysql"> "mysql5 server"> "MySQL Command Line Client"
// If this step is successful, mysql is safe. If the step is invalid, mysql installation fails. Uninstall and reinstall it.
 
B) enter the password.
// If this step is normal, the root account password is correct. If the password fails, the account password is incorrect.
 
2> create a file to the home directory and save it as testmysql. php, enter http: // localhost/testmysql in the browser. php (or http: // localhost: 81/testmysql. php)


If not, check if apache has loaded the php and mysql methods.


Add the following three lines to the httpd. conf file supported by apache support for php5:
PHPIniDir "D:/ccopen/php5 /"
LoadModule php5_module "D:/ccopen/php5/php5apache2_2.dll"
AddType application/x-httpd-php. php
Remove the following two semicolons from php. ini:
Extension = php_mysql.dll
Extension = php_mysqli.dll
Copy php_mysql.dll and libmysql. dll to system32
Restart apache server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.