I'm struggling all morning to make php work with an sqlite database. Here is a piece of php code that I try to execute:
#less /var/www/html/test.php
<?php
$db=new PDO("sqlite:/var/www/test.sql");
$sql = "insert into test (login,pass) values ('login','pass');";
$db->exec($sql);
?>
Here is how I've done tests:
# sqlite3 /var/www/test.sql
sqlite> create table test (login varchar,pass varchar);
#chown apache:apache /var/www/test.sql
#chmod 644 /var/www/test.sql
Here is the stuff that drives me mad: When I execute from command line:
#php test.php
everything goes well. Sql is being executed and I can see a new row appear in the database.
When I execute the same script from a browser - sql is not being executed. I don't get a new row in the database. There are no errors in the apache log file.
Please, help
UPDATE:
Ok, I've got the answer. It looks like sqlite is trying to create a new -journal file on "INSERT" query. strace rocks:
open("/var/www/test.sql-journal", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0644) = -1 EACCES (Permission denied)
So, I had to move sqlite database to a directory, apache could write to.
Commandline PHP and PHP being executed on a web server generally use a different php.ini file, so it's worth checking any differences. You may also have different versions of PHP installed, check that commandline and the webserver both have the same version. I've seen servers where Apache ran PHP5 but commandline php ran PHP4 unless you used the full path to the PHP5 binary.