PHP: Debugging PDO connection to Access database (.accdb)

杀马特。学长 韩版系。学妹 提交于 2019-11-28 12:39:38

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/24712708/php-debugging-pdo-connection-to-access-database-accdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!