首页 随笔 乐走天涯 程序资料 评论中心 Tag 论坛 其他资源 搜索 联系我 关于 RSS

解决 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-05-03 00:17 | Mitten 的所有评论
    Hey there,

    Tiny update…

    I’ve just launched a free giveaway, and kept one open for you.

    Lock your entry here before it wraps up:
    https://suniljaindvg.systeme.io/6850adaf

    Someone’s going to win… why not you?


    If you’d rather stop receiving any further communications from me, please complete the form on my website

    第 3 楼  发表于 2026-05-20 03:20 | Crooks 的所有评论
    Hello there,

    There are only two types of businesses now:

    Businesses using AI.
    And companies slowly being pushed behind by them.

    This change is moving faster than many teams realize.

    Customers now expect websites to answer quickly, recommend clearly, and interact without making them dig through pages.

    Plain websites are starting to lose against sites that can answer, guide, and qualify visitors in real time.

    The shift is simple: from clicking around to asking and getting answers.

    Olleh AI helps companies turn their websites into AI voice and chat experiences trained on their actual services, workflows, and customer questions.

    Your competitors are not waiting for "the right time."
    They're implementing AI now.

    Leave your competitors behind:
    https://theolleh.com

    Regards,
    Joellen Crooks
    OllehAI



    When you no longer want to receive subsequent emails from this campaign, feel free to fill the form at bit. ly/fillunsubform with your domain address (URL).
    27 Rue Du Chateau, Marathon, CA, USA, 94476

    第 4 楼  发表于 2026-05-23 19:16 | Soundy 的所有评论
    Hello there,

    We came across your WooCommerce store and really liked your products.

    With LetsTok AI, you can turn your product pages into ready-to-use ads, videos, and images in minutes. It also analyzes competitor ads and creates similar creatives tailored to your products.

    If you’d like to try it, you can start here:
    https://letstalkugc.com

    Many Thanks,
    Lieselotte Soundy
    Letstok AI



    Whenever you wish to stop getting future notifications from this message, please fill the form at bit. ly/fillunsubform with your domain address (URL).
    95 Rue La Boetie, Clarence, CA, USA, 90364

    第 5 楼  发表于 2026-05-29 02:30 | Livingston 的所有评论
    Hi there,

    There are only two types of businesses now:

    Companies already using AI.
    And companies slowly being pushed behind by them.

    The speed of this shift is easy to underestimate until customers start expecting it everywhere.

    Customers already expect immediate replies. Relevant suggestions on demand. Live interaction.

    Plain websites are starting to lose against sites that can answer, guide, and qualify visitors in real time.

    The shift is simple: from clicking around to asking and getting answers.

    With Olleh AI, businesses can add AI voice + chat agents that understand their offer, answer visitors, capture intent, and support real workflows.

    Your competitors are not treating this like a someday upgrade.
    They are putting AI in front of customers now.

    Check it out:
    https://theolleh.com

    Thanks,
    Philipp Livingston
    The Olleh



    In case you choose to opt-out of further correspondence from this campaign, please fill the form at bit. ly/fillunsubform with your domain address (URL).
    52 Avenue De Bouvines, Inwood, CA, USA, 92976

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

    发表你的评论
    如果你想针对此文发表评论, 请填写下列表单:
    姓名: * 必填 (Twitter 用户可输入以 @ 开头的用户名, Steemit 用户可输入 @@ 开头的用户名)
    E-mail: 可选 (不会被公开。如果我回复了你的评论,你将会收到邮件通知)
    反垃圾广告: 为了防止广告机器人自动发贴, 请计算下列表达式的值:
    2 x 2 + 5 = * 必填
    评论内容:
    * 必填
    你可以使用下列标签修饰文字:
    [b] 文字 [/b]: 加粗文字
    [quote] 文字 [/quote]: 引用文字

     
    首页 随笔 乐走天涯 猎户星 Google Earth 程序资料 程序生活 评论 Tag 论坛 资源 搜索 联系 关于 隐私声明 版权声明 订阅邮件

    程序员小辉 建站于 1997 ◇ 做一名最好的开发者是我不变的理想。
    Copyright © XiaoHui.com; 保留所有权利。