首页 > 解决方案 > 如何建立 Pervasive PDO PHP 连接

问题描述

我正在尝试建立与我的作品 Pervasive SQL 数据库的连接。我试过使用 odbc_connect (没用),但有人告诉我 PDO 更容易更好(HA,也没有用)。这是我的连接字符串 $dbh = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=192.168.43.19;dbq=GLOBALTST");

我试过 odbc:DSNname ( https://www.php.net/manual/en/ref.pdo-odbc.connection.php )、ODBC:servername (ip and hostname) 和 odbc:databasename。没有任何效果。这是我得到的错误:

Fatal error: Uncaught PDOException: SQLSTATE[IM003] SQLDriverConnect: 160 Specified driver could not be loaded due to system error 1114: A dynamic link library (DLL) initialization routine failed. (Pervasive ODBC Client Interface, C:\PSQL\bin\w3odbcci.dll). in C:\inetpub\wwwroot\default.php:4 Stack trace: #0 C:\inetpub\wwwroot\default.php(4): PDO->__construct() #1 {main} thrown in C:\inetpub\wwwroot\default.php on line 4

它正在寻找的 DLL 实际上位于它所引用的位置。据我所知,它没有问题。服务器上的 ODBC 配置正确,我可以连接到 PSQL 控制中心的数据库。任何人都可以帮助确定我的问题或指向适用于 odbc_connect 或 PDO 的连接字符串吗?

标签: phppdoodbcpervasivepervasive-sql

解决方案


此代码在 Windows 机器上使用 PSQL v11 64 位 ODBC、PHP 7.2 64 位对我有用。

<?php
try {
    // Connect to the data source
    //$dbh = new PDO($dsn);
    $dbh = new PDO("odbc:Driver={Pervasive ODBC Interface};ServerName=192.168.43.19;dbq=demodata");

    $stmt = $dbh->prepare('SELECT * FROM class');

    // Execute the prepared statement for each name in the array
    $stmt->execute();

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $numResults = count($result);
    echo ('<b>Total Results: </b> ' . $numResults . '<br>');
    if ($numResults > 0) {
        // Output the table header
        echo '<table><tr>';
        foreach ($result[0] as $fieldName=>$value) {
            echo '<th>' . htmlspecialchars($fieldName) . '</th>';
        }
        echo '</tr>';

        // Now output all the results
        foreach($result as $row) {
            echo '<tr>';
            foreach ($row as $fieldName=>$value) {
                echo '<td>' . htmlspecialchars($value) . '</td>';
            }
            echo '</tr>';
        }

        // Close the table
        echo '</table>';
    } else {
        echo 'No results';
    }
        

    // Close statement and data base connection
    $stmt = NULL;
    $dbh = NULL;
}

catch(PDOException $e) {
    echo $e->getMessage();
}
?>

推荐阅读