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.