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

前端 未结 2 2015
猫巷女王i
猫巷女王i 2020-12-22 12:20

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

2条回答
  •  臣服心动
    2020-12-22 12:38

    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.

提交回复
热议问题