0

我正在开发 PHP 并有一个简单的文件来测试各种功能。为了方便起见,我想从命令行运行这个文件。这导致了一个问题,我不确定它是什么。

该代码使用 PDO。我有一个数据库类,它充当 PDO 包装器并强制执行单例设计模式。课程非常简单。以下是一些相关代码:

// instance of PDO
private static $db = null;

//connection related fields
const dsn = "mysql:dbname=mydb;host=localhost";
const user = "myuser";
const password = "mypass";

//get access to the PDO instance
public static function get_instance()
{
    if(!self::$db)
    {
        self::$db = new PDO(self::dsn,self::user,self::password);
        self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        self::$db->exec("set names utf8");
    }

    return self::$db;

}

现在,当我使用以下内容创建测试文件时:

$db = Database::get_instance();

并像这样从命令行运行它:

php5 ./testing.php

我得到一个异常:带有消息“找不到驱动程序”的 PDOException' 这是由上面实例化新 PDO 对象的行引起的。

但是,如果我通过浏览器导航到该文件并且它由我的 Web 服务器提供服务,它运行良好,则该对象按预期工作。

This appears to be an issue with running a PHP file from the terminal. I don't think this is an issue in code. Could anyone suggest what could be causing this? Any advice or help would really be appreciated. Thanks.

4

2 回答 2

3

The commandline version of PHP can have a different configuration than the web-version. Locate the proper PHP configuration file, setup the database drivers and you should be fine.

于 2012-04-14T19:04:08.220 回答
2
/usr/local/php/bin/php -> /usr/bin/php

/usr/bin/php is commandline version of PHP, /usr/local/php/bin/php is web version

try ln command

于 2012-09-06T08:26:32.040 回答