Wine Too
Wine Too

Reputation: 4655

SQLite on localhost with php doesn't work (Linux)

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.

Upvotes: 0

Views: 3131

Answers (1)

jdhartley
jdhartley

Reputation: 486

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 ....";

Upvotes: 3

Related Questions