2

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.

facha
  • 1,298
  • 2
  • 16
  • 26
  • Do you normally access your system as root? (the '#' prompt usually implies root). If so then stop it. Work out a security model where you don't need to tinker with permissions like this. – symcbean Jun 26 '14 at 15:02

1 Answers1

0

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.

WheresAlice
  • 5,290
  • 2
  • 23
  • 20
  • Have just checked that. Same php.ini. Same php version. In general, it would look like a problem with permissions. I can execute "SELECT" queries from apache user, but not "INSERT INTO". – facha May 24 '10 at 12:37