3

I am able to retrieve information from a SQLite database in PHP, but not write to it. For example, this code works perfectly fine:

$db = new PDO("sqlite:foo.db");
$rowCount = $db->query("SELECT COUNT(*) FROM tblname;")->fetchColumn(0);
echo $rowCount; // works as it should

However, the following snippet results in an, 'unable to open database file' error:

$db = new PDO("sqlite:foo.db");
$db->exec("INSERT INTO tblname VALUES(NULL, 'val1', 'val2');") or die(print_r($db->errorInfo(), true));

When I perform the above INSERT from the command line rather than in PHP, I get the expected results (a new row in tblname):

$ sqlite3 foo.db
sqlite> INSERT INTO tblname VALUES(NULL, 'val1', 'val2');

This is my first time using PDOs (or SQLite for that matter) so any help would be greatly appreciated.

1
  • Does your web server (apache/php or whatever) have write access to the database? Commented Jan 11, 2010 at 0:12

2 Answers 2

12

Check that the user running the PHP script has permission to write foo.db.

Also (from memory, correct me if I'm wrong) the user will need write permission on the directory containing foo.db, or else SQLite3 will throw a not-so-useful error message.

Sign up to request clarification or add additional context in comments.

4 Comments

I made sure the database and the directory it resides in are both writable but that didn't seem to change anything.
Try using an absolute path to foo.db in your code, just to rule out any working directory differences that may be happening...
"Check that the user running the PHP script..." That was the problem. My normal user was the one with write permissions, not the user that was actually running the script. Thanks.
Wow, it was the folder permission for me, that was unexpected but fixed. Isn't that makes it more dangerous for attacks?
2

You probobly have no write acccess to the sqllite file. Check the error message you get form PDO with PDO::errorInfo

Comments

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.