1
votes

I am new to programming and would like to connect to a ms-access (accdb) database using a PDO class. Environement: PHP (5.5.11) / XAMPP / Windows 7 pro. PDO driver for ODBC (win32) is enabled.

class db{
  protected $dbName = "C:\xampp\htdocs\BillboardsManagement\Core\config\Billboards.accdb";
  protected $Uid="";
  protected $Upass="";
  protected $conn;

  public function __construct() {
    try{
        $this -> conn = new PDO('odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=$this->$dbName;Uid=$this->$Uid;Pwd=$this->$Upass');
    } catch (Exception $e) {
        echo "\n $e-> getMessage()\n";   
    }
  }
}

When I try to instantiate the class, I get the following error:

exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified' in C:\xampp\htdocs\BillboardsManagement\Core\config\config.php:13 Stack trace: #0 C:\xampp\htdocs\BillboardsManagement\Core\config\config.php(13): PDO->__construct('odbc:DRIVER={Mi...') #1 C:\xampp\htdocs\BillboardsManagement\Views\selectBB.php(3): db->__construct() #2 {main}-> getMessage() Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\BillboardsManagement\Core\classes\bbClasses.php on line 11

Thanks in advance for your help.

Updates: I understand that a similar question was answered before. But I am in a learning process. The answer to the previous post was to use adodb instead of PDO ( for reasons I totally agree with) , but I am still curious about what went wrong in my particular situation. I still cannot determine whether my code was faulty or it was some odbc driver or configuration issue.

2
I've moved and expanded my previous comment into an answer of its own since since it apparently wasn't clear enough. But please don't just ignore comments you don't understand—ask for clarifications instead. Nobody likes his help attempts to become wasted time.Álvaro González

2 Answers

5
votes

You provide the connenction details in this string:

'odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=$this->$dbName;Uid=$this->$Uid;Pwd=$this->$Upass'

If we check how strings work in PHP we can read this about our single-quoted string:

Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

So PHP will try to open a file called $this->$dbName, literally. So, to begin with, you possibly want one of these syntaxes instead:

  • Double-quoted string: "File name $foo blah"
  • Concatenation: 'File name ' . $foo . ' blah'

Now, you want to read the file name from an object property:

protected $dbName = "C:\xampp\htdocs\BillboardsManagement\Core\config\Billboards.accdb";

The manual explains that the syntax is:

$this->dbName

However, you have this:

$this->$dbName

Once you fix this, it's always a good idea to verify whether your variables contain what you think they do. For that, I recommend var_dump():

$connection_string = 'odbc:DRIVER={Microsoft ....';
var_dump($connection_string);

It's worth noting that everything I've explained is related to basic PHP syntax. Neither PDO nor Access issues have come up so far. It always help to isolate your issues.

3
votes

After lots and lots of reading, I discovered I was using the wrong version of the Microsoft Access Database Engine: The 64-bit version doesn't have the 32-bit driver for *.accdb format.

Thanks again for your enlightening support.