Search code examples
phppervasive-sql

PHP Pervasive SQL connection


I am connecting PHP to Pervasive SQL and the connection keep resetting

This connection is on Windows Server 2012 using PHP7 on Apache 2.4. I have already created a DNS connection and the test can connect to the database successfully.

<?php
  $conn=odbc_connect("brps","","");
   if(!$conn) die("Could not connect");
?>

Solution

  • The following code works for me in an x64 environment using PHP 7.31 with the ODBC extension enabled in the PHP.INI. I'm also using the v11.30 x64 Client connecting to a remote PSQL v11 server.

    <?php
      $conn=odbc_connect("brpp","","");
       if(!$conn) die("Could not connect");
    $result = odbc_tables($conn);
    
    echo '<div id="top">..</div><table border="1" cellpadding="5"><tr>';
    
    $tblRow = 1;
    while (odbc_fetch_row($result)){
      if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
        $tableName = odbc_result($result,"TABLE_NAME");
        echo '<tr><td>' . $tblRow . '</td><td><a href="#' . $tableName . '">' . $tableName . '</a></td></tr>';
        $tblRow++;
      }  
    }
    echo '</table><hr>';
    
    $result = odbc_tables($conn);
    
    while (odbc_fetch_row($result)){
      if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
        $tableName = odbc_result($result,"TABLE_NAME");
    
        echo '<div id="' . $tableName . '"> *** ' . $tableName . ' *** <a href="#top">top</a></div>';
    
        $cols = odbc_exec($conn, "SELECT * FROM $tableName WHERE 1=2");
        $ncols = odbc_num_fields($cols);
    
        for ($n=1; $n<=$ncols; $n++) {
          $field_name = odbc_field_name($cols, $n);
          echo $field_name . "<br>";
        }    
        echo '<hr>';
      }
    }
    ?>
    

    If the DSN doesn't exist, I get an error:

        Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\Wnmp\html\phpodbc.php on line 2
    Could not connect
    

    I was getting a System error 998 after installing the PSQL x64 client. A reboot of the machine fixed that error.