Home  >  Article  >  Backend Development  >  Technical notes on solving the problem that PHP cannot connect to MySQL under the DOS command line_PHP tutorial

Technical notes on solving the problem that PHP cannot connect to MySQL under the DOS command line_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:38894browse

It just so happened that my friend xjb also encountered this problem today, so I wrote this note to record the description and solution of the problem.
Problem description: Using the web method, you can connect to mysql, but under the command line, it prompts:
Fatal Error: undefined function mysql_connect()
Environment: Windows 2003, PHP 5.2.0, MYSQL 5.0, Apache 2.0
In php.ini, the module option of php_mysql.dll has been turned on; the test script is also very simple, just a mysql_conect function, the content is as follows:

Copy the code The code is as follows:

php.ini:
extension=php_mysql.dll
The content of test script test.php 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.
But use the dos command command Calling d:/php/php.exe test.php via line shows that the connection failed. The error message is: Fatal Error: undefined function mysql_connect()
Obviously, in the dos command line environment, the mysql module was not called. I tried all the methods, but still couldn't solve it. I was puzzled. Later, I wrote a script to see the difference in the php configuration in the two environments:
test.php
Copy code The code is as follows:

echo phpinfo();
?>


Carefully check the php configuration information input by phpinfo() in the two environments, and finally found the problem:
When calling http://localhost/test.php in Web mode, its Configuration File (php.ini ) Path is shown as C:WINDOWSphp.ini.
When DOS COMMAND command line mode d:/php/php.exe test.php is called, its Configuration File (php.ini) Path is d:phpphp.ini.
There is a php.ini file in both the c:windows and d:php directories. The php.ini in the d:php directory does not open the extension=php_mysql.dll module. So I deleted d:phpphp.ini and kept only c:windowsphp.ini, and the problem was solved.
Summary: When installing php under windows, in order to avoid configuration file confusion, after copying the php.ini in the installation directory to the windows directory, you should rename or delete the php.ini file in the original installation directory to avoid This causes inconsistencies in finding configuration files when executing in different environments. The cause of the problem is usually very simple, but the process of finding and troubleshooting it is quite annoying.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322737.htmlTechArticleJust today my friend xjb also encountered this problem, so I wrote this note to describe the problem and solve it Record it. Problem description: Using the web method, you can connect to mysql, but...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn