Solve the technical notes that PHP cannot connect to MySQL under the doscommand line
Source: Internet
Author: User
Some time ago, when php was used for Shell programming, it encountered the problem that PHP could connect to MySQL on the WEB, but the connection failed on the DOSCOMMAND command line. Today, my friend xjb also encountered this problem. so I wrote this note and recorded the description and solution of this problem.
Problem description: a web interface can be used to connect to mysql, but the following prompt is displayed on the command line:
Fatal Error: undefined function mysql_connect ()
Environment: Windows 2003, PHP 5.2.0, MYSQL 5.0, Apache 2.0
In php. ini, you have enabled the module option of php_mysql.dll. the test script is also very simple. it is a mysql_conect function with the following content:
The code is as follows:
Php. ini:
Extension = php_mysql.dll
The content of the test. php test script is as follows:
Text. php
If (! Mysql_connect (DBHOST, DBUSER, DBPWD ))
{
Echo "connection failed! ";
Exit;
}
Echo "connection successful! T ";
?>
Use the web method to call http: // localhost/test. php. the execution is normal and "connection successful" is displayed ".
However, the dos command line call d:/php/php.exe test. php shows a connection failure. the Error message is: Fatal Error: undefined function mysql_connect ()
Obviously, mysql modules are not called in the dos command line environment. The solution is exhausted, but it still cannot be solved. I was puzzled. later, I wrote a script to check the php configuration. what is the difference between the two environments:
Test. php
The code is as follows:
Echo phpinfo ();
?>
Check the php configuration information entered by phpinfo () in two environments, and finally find the problem:
When you call http: // localhost/test. php in Web mode, the Configuration File (php. ini) Path is displayed as C: WINDOWSphp. ini.
Dos command line mode d:/php/php.exe test. php, the Configuration File (php. ini) Path is d: phpphp. ini.
In the c: windows and d: php directories, there is a php. ini file, d: php under the php directory. ini did not open the extension = php_mysql.dll module. then d: phpphp. ini deletion. only c: windowsphp is retained. ini to solve the problem.
Summary: To avoid confusion in the configuration file when installing php in windows. after the ini file is copied to the windows directory, you should copy the php file in the original installation directory. rename or delete the ini file to avoid inconsistent search configuration files during execution in different environments. The cause of the problem is generally very simple, but the process of searching and troubleshooting is quite annoying.
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.