1

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.

1
  • 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. Commented Jul 13, 2014 at 9:07

2 Answers 2

5

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.

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

1 Comment

Your comments and detailed help have been really appreciated and it was not my intention to let you believe you're wasting your time, I'm really sorry about this. Following your advices I am more and more convinced it is an odbc driver issue. I just cannot find a 32-bit driver for accdb databases on 64-bit platform.
3

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.

1 Comment

I am facing the same issue with @Phil, but I am using wampserver3 64bit, I just uninstall 64bit and install wampserver 32bit and it is working fine now.

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.