0

I am developing PHP and have a simple file to test various functionality. To facilitate this I would like to run this file from the command line. This is causing a problem and I'm not sure what it is.

The code makes use of PDO. I have a Database class that acts as a PDO wrapper and enforces a singleton design patter. The class is very simple. Here is some of the relevant code:

// 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;

}

Now when I create a test file with the following:

$db = Database::get_instance();

and run it from the command line like so:

php5 ./testing.php

I get an exception: PDOException' with message 'could not find driver' this is caused by the line above that instantiates a new PDO object.

HOWEVER if I navigate to this file via my browser and it is served by my web server it runs fine, the object works as expected.

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.

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.