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.

7
  • possible dup of stackoverflow.com/questions/1513849/… Commented Apr 29, 2012 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. Commented Apr 29, 2012 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. Commented Apr 29, 2012 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. Commented Apr 29, 2012 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. Commented Apr 29, 2012 at 16:28

1 Answer 1

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 ....";
Sign up to request clarification or add additional context in comments.

1 Comment

PDO dont want to work too: SQLSTATE[HY000] [14] unable to open database file Database -- NOT -- loaded successfully .. Query Closed !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.