解决 PHP 在 DOS 命令行下却无法链接 MySQL 的技术笔记


日期: 2007-03-28 14:00 | 联系我
关注我: Telegram, Twitter

  前段时间,由于要用 php 进行 Shell 编程时,碰到了 PHP 在 WEB 下可以连接 MySQL 而在 DOS COMMAND 命令行下却连接失败的问题。正好今天朋友 xjb 也碰到了这个问题,所以写了这篇笔记,将此问题的描述以及解决记录在 https://www.xiaohui.com/dev/server/20070328-php-mysql-connect.htm

  问题描述:用 web 方式, 可以链接 mysql, 但是在命令行下, 却提示:

  Fatal Error: undefined function mysql_connect()

  环境: Windows 2003, PHP 5.2.0, MYSQL 5.0, Apache 2.0

  在 php.ini 中, 已经将 php_mysql.dll 的模块选项给打开了;测试的脚本也很简单,就一个 mysql_conect 函数,内容如下:

php.ini:
extension=php_mysql.dll 

测试脚本 test.php 内容如下:
text.php 
<?
if ( !mysql_connect(DBHOST, DBUSER,DBPWD) ) 
{ 
  echo "连接失败!"; 
  exit; 
} 
echo "连接成功!t"; 
?>

  用 web 方式调用 http://localhost/test.php, 执行正常, 显示"连接成功".

  但用 dos command 命令行的方式调用 d:/php/php.exe test.php 却显示连接失败, 错误信息为: Fatal Error: undefined function mysql_connect()

  很显然, 在 dos command 命令行的环境下,mysql 的模块没有被调用。方法弄尽, 仍无法解决. 百思不得其解. 后来, 用写了个脚本, 看看 php 的配置, 在两个环境下有什么不同:

test.php
<?
echo phpinfo();
?>

  仔细查看两个环境下 phpinfo() 输入的 php 配置信息,终于发现了问题所在:

  • Web 方式 http://localhost/test.php 方式调用时, 其 Configuration File (php.ini) Path 显示为 C:WINDOWSphp.ini。
  • DOS COMMAND 命令行方式 d:/php/php.exe test.php 调用时, 其 Configuration File (php.ini) Path 为 d:phpphp.ini。

  而在 c:windows 和 d:php 目录下, 都存在一个 php.ini 文件,d:php 目录下的 php.ini 没有将 extension=php_mysql.dll 模块打开.于是将 d:phpphp.ini 删除,只保留 c:windowsphp.ini,问题解决.

  总结:在 windows 下安装 php 时, 为了避免配置文件混乱,将安装目录下的 php.ini 复制到 windows 目录下之后,应该将原来安装目录下的 php.ini 文件重命名或删除,以免造成在不同环境下执行时,查找配置文件不一致的情况发生。问题发生的原因一般都是非常非常简单,但查找、排除问题的过程,却是相当的烦人。

相关文章

  • PHP SQL 注入攻击的技术实现以及预防办法
  • 解决一个 MySQL 服务器进程 CPU 占用 100%的技术笔记
  • HipHop for PHP
  • 标签: XJB | Apache | MySQL | PHP

     文章评论

    第 1 楼  发表于 2007-03-29 13:58 | braveheart 的所有评论
    很实用的技术文章,请小辉多多发表,呵呵....

    第 2 楼  发表于 2026-09-03 04:15 | Dehaven 的所有评论
    Blind upgrades are the quickest way to waste budget on hardware. The real issue is almost always a hidden bottleneck, CPU, GPU, or memory imbalance.

    https://brnd.li/fpsbenchmarks

    https://pristinetraffic.com/rate-my-pc

    FPSBench evaluates real-world data to show system behavior across games, helping you pinpoint exactly what’s limiting your FPS and what upgrade actually moves the needle.

    Avoid overspending, optimize upgrades using real data.



    Should you wish to stop getting subsequent notifications from this campaign, feel free to fill the form at brnd .li/delist url with your domain address (URL).
    Vicolo Calcirelli 67, Clifton Park, CA, USA, 91673

    第 3 楼  发表于 2026-09-10 16:05 | Tindale 的所有评论
    Hey there,

    Restore any site from Web Archives (Wayback Machine).

    Restore (any time interval like from 1999 March to 2026 September):
    - Your website's full content: pages, css, html, and images
    - Your lost pages
    - Expired domains
    - PBN content


    Cheapest yet the fastest and most reliable solution.

    200 pages restore costs 12 cents.

    @@ Restore your sites now: https://bynd.li/time-machine

    * Wordpress restore option (included)




    In case you wish to stop getting any more correspondence from me, just fill the form at brnd .li/delist url with your domain address (URL).
    Ybbsstrasse 73, Bridgehampton, CA, USA, 94877

    共有评论 3 条, 显示 3 条。

    当前页面是本站的 百度 MIP 版本。
    欲查看完整版本和发表评论请点击:完整版 »

     

    程序员小辉 建站于 1997
    Copyright © XiaoHui.com; 保留所有权利。