I am trying to establish a connection to my works Pervasive SQL database. I've tried using odbc_connect (didn't work) but I was told that PDO is easier and better (HA, also didn't work). This is my connection string
$dbh = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=192.168.43.19;dbq=GLOBALTST");
I've tried odbc:DSNname (https://www.php.net/manual/en/ref.pdo-odbc.connection.php), ODBC:servername (ip and hostname), and odbc:databasename. Nothing has worked. This is the error I am getting:
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
The DLL it is looking for is actually located in the spot it's referencing. as far as I can tell there are no issues with it. THe ODBC is configured correctly on the server and I can connect to the DB in the PSQL control center. can anyone help in identifying my issue or pointing to a connection string that works either odbc_connect or PDO?
This code worked for me using PSQL v11 64 bit ODBC, PHP 7.2 64 bit on a Windows machine.
<?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();
}
?>
Related
I have a PHP codebase that resides on a Windows server that connects to an Oracle database. My database is moving to a Oracle in the cloud setup that requires us to use an Oracle wallet to connect.
I got the setup for the wallet and can connect via the utilities, but all my research thus far has me using oci_connect() with a session_mode of OCI_CRED_EXT. But PHP's oci_connect (https://www.php.net/manual/en/function.oci-connect.php) states "OCI_CRED_EXT is not supported on Windows for security reasons.".
Is there a way to utilize an Oracle wallet via PHP on windows?
The Oracle Cloud wallets provide mutual TLS for security but you still need to pass a DB username and password. Hence you don't use external authentication.
You just need to make sure that Oracle Net can find the wallet, either with the Easy Connect Plus syntax used below (wallet_location is the dir containing the cwallet.sso file), or by editing sqlnet.ora (or putting it in a default location). See How to connect to Oracle Autonomous Cloud Databases.
Code that works for me is:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$c = oci_connect('cj', 'topsecret', 'tcps://adb.ap-melbourne-1.oraclecloud.com:1522/xxxxxx.adb.oraclecloud.com?wallet_location=/Users/cjones/Cloud/alwaysfree/CJDBMELB');
if (!$c) {
$m = oci_error();
trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}
$s = oci_parse($c, "select * from dual");
if (!$s) {
$m = oci_error($c);
trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
$m = oci_error($s);
trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}
while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo "<tr>\n";
foreach ($row as $item) {
echo "<td>";
echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):" ";
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
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");
?>
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>' . $tableName . '</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 . ' *** top</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.
i am trying to realize an exercice with Ajax and Sqlite database for which i use DB Browser for SQLite. The purpose of the exercice is to have two combobox fill each other in cascade. But first of all i need to fill the first one from data that i gather from the Database. But i am blocked on that first step.
Here is the code i made until now :
<?php
try
{
$db = new
PDO('sqlite3:\C:\xampp\htdocs\www\TPLigueDesChampions\ligueChampions.db');
$result = $db->query('SELECT pays FROM listepays');
echo ' <select name=\'Code\'><br>';
echo ' <option value=\'0\'>Choisissez</option>';
foreach($result as $row)
{
echo '<option value=\''.$row['Pays'].'<option><br>';
//echo "hello";
}
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
So when i make sqlite3 in pdo call i have the following error:
"Could not find driver"
And when i use sqlite normal it gives me out this: SQLSTATE[HY000] [14] unable to open database file
I searched in internet about a solution but everything i found until now is related to MySQL and i need SQLite. Can you please help?
for php
<?php
$bd = new SQLite3('ligueChampions.db');
$results = $bd->query('SELECT pays FROM listepays');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
SQLite3 and Php
I'm really in need of assistance with this issue, ive been trying for days. I have an access database which i'm trying to query via PHP - i have come to this forum because after reading many posts and trying different things i am certain that this is some kind of issue on the server.
I have installed Microsoft access database engine, both 2010 and the 2007 version but whatever i try i get the same error message :
Uncaught exception 'com_exception' with message '<b>Source:</b> ADODB.Connection<br/><b>Description:</b> Provider cannot be found.
I have uncommented all the relative lines in php.ini (after crawling through many stack overflow posts with the same issue) but with no success. Below is the code i am using in my php file.
<?php
// Create an instance of the ADO connection object
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
// Define the connection string and specify the database driver
$connStr = "PROVIDER=Microsoft.Ace.OLEDB.12.0;Data Source=".realpath("HS_BE.accdb").";";
// Open the connection to the database
$conn->open($connStr);
// Declare the SQL statement that will query the database
$query = "SELECT * FROM Accommodation";
// Execute the SQL statement and return records
$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();
echo $num_columns . "<br>";
for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
}
echo "<table>";
while (!$rs->EOF) // Carry on looping through while there are records
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $fld[$i]->value . "</td>";
}
echo "</tr>";
$rs->MoveNext(); // Move on to the next record
}
echo "</table>";
// Close the connection and recordset objects freeing up resources
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
Id be very grateful for some assistance!
I have a sqlite db file which is definitely not corrupt since I can open it with SQLiteStudio.
However, when I try to open it dynamically with PHP with the following code I found in some tutorial:
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../testDB');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql ="SELECT * from testTable";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) )
{
echo "ID = ". $row['id'] . "\n";
echo "NAME = ". $row['name'] ."\n\n";
}
echo "Operation done successfully\n";
$db->close();
I get the following result:
Opened database successfully
Warning: SQLite3::query() [sqlite3.query]: Unable to prepare statement: 11, database disk image is malformed in test.php on line 52
Fatal error: Call to a member function fetchArray() on a non-object in test.php on line 53
I found some threads like that one, but none of them had a definite answer.
Can somebody help me out here? Thanks in advance!
I had this same "database disk image is malformed" error and i solved it using SQLiteStudio.
Since you already have it, open the file, right click on the database and try the Vacuum option. After doing this, try the integrity check. If the result is 'OK' then your problem is solved. At least that's how i solved it. Vacuum rebuilds the entire database.
I've already the same problem with small SQLITE db.
Try to use:
$sql = "VACUUM";
$db->query($sql);