PDO returning error “could not find driver” with a known working DSN

后端 未结 1 861
栀梦
栀梦 2020-12-03 14:13

I\'m trying to connect to an odbc database via php\'s PDO class:

$dsn = \'odbc:CS_HDZipCodes32bit\';
$username = \'demo\';
$password = \'skdemo!         


        
相关标签:
1条回答
  • 2020-12-03 14:51

    After getting some great help from Marc B in the initial question's comments it turns out the problem was coming from my misunderstanding of enabling odbc on my web server and having the pdo_odbc driver installed.

    While I did have odbc enabled on the web server, I did not have the odbc driver installed for PDO.

    missing odbc driver

    This driver is necessary to be able to access odbc databases via pdo.

    Per the php.net documentation on installing pdo for windows, the driver was already included in the php build (I'm using version 5.5) and just needed to be included in the php.ini file.

    After adding the driver and restarting the server I now had the driver loaded:

    and my test query using PDO on my demo database worked:

    $dsn = 'odbc:CS_HDZipCodes32bit';
    $username = 'demo';
    $password = 'skdemo!';
    
    $connection = new PDO($dsn, $username, $password);
    
    $query = "Select * FROM ZipCodes";
    
    
    $result = $connection->query($query);
    
    foreach($result as $row){
        var_dump($row);
    }
    

    Dumps out:

    Thanks for your help Marc B.

    0 讨论(0)
提交回复
热议问题