Sqlite3 PHP "database disk image is malformed" - php

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);

Related

How to make Pervasive PDO PHP connection

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();
}
?>

Ajax Sqlite could not find driver./SQLSTATE[HY000]

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

php odbc close connection is not working properly

The connection cannot close after i open it. Anyone knows how to solve my problem?
I have search a lot of possible answer but no one solve it.
This is my code
<?php
include 'db.php';
$sql = "SELECT * FROM Table";
$res = odbc_exec($conn, $sql);
while(odbc_fetch_array($res)){
# .....
}
odbc_close($conn);
if (odbc_close($conn)) {
echo "Connection closed";
}else{
echo "Connection was not close";
}
?>
It says The connection was not close.
You can not put odbc_close($conn) in if condition. It never returns anything. its return type is void so if condition will never return true for this.
for more reference please read this.
PHP.net odbc
you can use is_resource($con). it gives false if the connection is closed.

PHP.ini causes Warning: mysqli_error() expects parameter 1 to be mysqli, null given in/sys/index.php on line 19 [duplicate]

This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
this script works fine on Local Host and Other free Hosting but not on "host Gator "it should be " PHP.ini " Any Help !?!?
function.php
<?php
date_default_timezone_set("America/Chicago");
echo date("Y/m/d H:i:s");
function consql($con){
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));}
?>
index.php
line #1 "Very top "
<?php
SESSION_START();
require_once"inc/function.php";
consql($con);
?>
line#12
<?php
if(isset($_SESSION['user']))
{
header("location:users.php");
}
elseif(isset($_POST['submit']))
{
$result=mysqli_query("select user_id,password from user_login where name='$_POST[username]'") or die(mysqli_error($con)); // ** line # 19 ** //
$n=mysqli_fetch_assoc($result);
if($n['password']!=$_POST['userpassword'])
{
echo "user name or password wrong";
}
else
{
echo "you loged in as $_POST[username]";
$_SESSION['user']=$n['user_id'];
header("location:users.php");
}
}
else
I'm new to mysqli and looked up here for same error but didn't get the point on most of them.
You can use
function consql(&$con){
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));
}
So this is call by reference and $con can use at any page once you call the function
Or use global $con; before the connection line
function.php
function consql()
{
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));
return $con;
}
index.php
require_once"inc/function.php";
$con = consql();
explanation :
why no error appears in your localhost‌? is it a PHP.ini problem ?
maybe errors are hidden on your localhost or mysqli_query runs with no problem there.
but there is a problem on your host, such as connection problem, missing or broken database table or etc...
base on your code, when no error occurs, mysqli_error() wont run and you dont get error

PHP and HTML: 000webhost error

Im building my website on 000webhost and with Zyro Builder and now i need some php code to get data from mysql database.
Im having this error: "Parse error: syntax error, unexpected '?' in ....." every time i want to use something like this:
echo "<br/>";
I know that is possible, so why I can't use it? :S
Thank you for your attention.
<?php
// Connect to database server
$con=mysqli_connect("mysql4.000webhost.com","a8373599_gus","******","a8373599_lolgus") or die(mysql_error($con));
if(mysqli_connect_errno($con)) {
echo "Fail to connect: " . mysqli_connect_errno();
} else {
echo "sucess!";
}
$result = mysqli_query($con, "SELECT * FROM Player");
$row = mysqli_fetch_array($result);
echo "<br/>";
echo $row['UserName']." ".$row['Playername'];
// Close the database connection
mysqli_close($con);
?>
Try using the numerical instances,
Eg:
echo $row[0]." ".$row[1];

Categories