0

I am trying to get my fist sqlite programs with php working on localhost but I can't get it to work. On my machine I have installed Sqlite3 and all works fine with C/C++.

If I move created database to localhost, give read/write permissions to db file and try to access them through php I get following error message:

file is encrypted or is not a database

Here is example code I use:

<?php
$dbhandle = sqlite_open("m_test1.db", 0666, $error);
if (!$dbhandle) die ($error);

$stm = "CREATE TABLE Friends(Id integer PRIMARY KEY," . 
   "Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')))";
$ok = sqlite_exec($dbhandle, $stm, $error);

if (!$ok)
   die("Cannot execute query. $error");

echo "Database Friends created successfully";
sqlite_close($dbhandle);
?>

If I run this code through browser when database don't exists then I get:

unable to open database: /var/www/m_test1.db

Info:

sqlite_libversion: 2.8.17
phpversion: 5.3.2-1ubuntu4.14
linux Ubuntu 10.04

By looking to phpinfo it seems that SQLite, SQLite3 and PDO_Sqlite is enabled.

Any help to get this working will be appreciated.

EDIT: Solution is: 'chmod ugo+rwx /var/www' :)
After that sqlite_open and PDO both can create database.

Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • possible dup of http://stackoverflow.com/questions/1513849/error-file-is-encrypted-or-is-not-a-database – jdhartley Apr 29 '12 at 16:01
  • @jdhartley: there certainly are dupes of this question, but the one you mention is not one of them; it's not even the same error. –  Apr 29 '12 at 16:16
  • @sixfeetsix How is this not the same error? PHP is saying the file is encrypted or not a database, and the solution is he needs to use PDO with PHP5. – jdhartley Apr 29 '12 at 16:18
  • @sixfeetsix The other question does not specify which line of code causes the error, and from personal experience this error is caused by using sqlite_open with PHP5. – jdhartley Apr 29 '12 at 16:26
  • @jdhartley: yes sorry I was looking at some other thread assuming it was your dupe, sorry bout that. I still disagree that the other question is an _exact_ dupe. –  Apr 29 '12 at 16:28
  • @sixfeetsix I answered so we can point him in the correct direction. If it's an exact dup it'll get closed, if not he gets his answer. :) – jdhartley Apr 29 '12 at 16:32
  • @jdhartley: yes dupe or not +1 on your answer; and since the OP says he only assigned permissions to the db file, I'm assuming he'll ask something related to this as well http://stackoverflow.com/questions/1485525/unable-to-write-to-a-chmod-777-database-file-on-sqlite3-via-php-5-3-0 –  Apr 29 '12 at 16:33

1 Answers1

3

PHP5 doesn't play nice with sqlite_open(). You'll need to use a PDO instead, like shown here: https://stackoverflow.com/a/4751965/369032

(code copied from above answer)

try 
{
    /*** connect to SQLite database ***/

    $dbh = new PDO("sqlite:VPN0.sqlite");
    echo "Handle has been created ...... <br><br>";

}
catch(PDOException $e)
{
    echo $e->getMessage();
    echo "<br><br>Database -- NOT -- loaded successfully .. ";
    die( "<br><br>Query Closed !!! $error");
}

echo "Database loaded successfully ....";
Community
  • 1
  • 1
jdhartley
  • 486
  • 3
  • 11
  • PDO dont want to work too: SQLSTATE[HY000] [14] unable to open database file Database -- NOT -- loaded successfully .. Query Closed !!! – Wine Too Apr 29 '12 at 18:53