Solve PHP in the DOS command line but could not link to MySQL technical notes

  • 2020-03-31 21:22:19
  • OfStack

Just today, my friend XJB also encountered this problem, so he wrote this note and wrote down the description and solution of this problem.
Problem description: in web mode, you can link to mysql, but at 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 for php_mysql.dll has been opened. The test script is also very simple, just a mysql_conect function, as follows:
 
php.ini: 
extension=php_mysql.dll 
 The test script  test.php  It reads as follows:  
text.php 
<? 
if ( !mysql_connect(DBHOST, DBUSER,DBPWD) ) 
{ 
echo " The connection fails !"; 
exit; 
} 
echo " The connection is successful !t"; 
?> 

Call http://localhost/test.php web way, implement the normal, show "connection is successful.
But calling d:/ PHP /php.exe test.php from the DOS command line shows that the connection failed, and the Error message is: Fatal Error: undefined function mysql_connect()
Obviously, in a DOS command line environment, the mysql module is not invoked. Later, I wrote a script to see how PHP was configured in the two environments:
Test. The PHP
 
<? 
echo phpinfo(); 
?> 


Looking closely at the PHP configuration information entered by phpinfo() in both environments, I finally found the problem:
http://localhost/test.php Web way way call, the Configuration File (the php.ini) show the Path to C: WINDOWSphp. Ini.
The Configuration File (php.ini) Path is d:phpphp.ini.
In the c: Windows and d: PHP directory, there is a php.ini file, d: PHP directory php.ini did not open the extension=php_mysql.dll module. Then d:phpphp.ini was deleted, leaving only c:windowsphp.ini, the problem was solved.
Summary: when installing PHP under Windows, after copying php.ini from the installation directory to the Windows directory, you should rename or delete the php.ini file from the original installation directory to avoid inconsistent configuration files when executing in different environments. The cause of the problem is usually very, very simple, but the process of finding and eliminating the problem is quite annoying.

Related articles: