PHP error SQLite3 :: exec

I get an odd error in a PHP script when it comes to SQLite3 :: exec, the script can connect to the database file without problems, and I can make a query without problems, but when I try to make an insert query, SQLite3 :: lastErrorMsg has this error "could not open the database file." Here is an example of how I am trying to do this:

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { add(); } else { mainpage(); } function mainpage() { $db = db_connect(); $results = $db->query('SELECT * FROM devices'); // Works just fine while (list($id, $name, $ip, $addr, $status) = $results->fetchArray()); print "ID: $id, Name: $name, IP Address: $ip, MAC Address: $addr, Status: $status"; } $db->close(); } function add() { $db = db_connect(); $query = $db->exec("INSERT INTO devices (name,ip,address,status) VALUES ('BB1', '192.168.1.5', '01:2D:45:AD:D3:A0', '1')"); if (!$query) { die("Database transaction failed: " . $db->lastErrorMsg() ); } mainpage(); $db->close(); die; } function db_connect() { class DB extends SQLite3 { function __construct( $file ) { $this->open( $file ); } } $db = new DB('devices.db'); if ($db->lastErrorMsg() != 'not an error') { print "Database Error: " . $db->lastErrorMsg() . "<br />"; //Does not get triggered } return $db; } ?> 

I know that in the add function it can connect to the database, because the if in the db_connect function never starts, and if I change the permissions on the devices.db file to read only, exec causes an error in writing to the read-only database .

+4
source share
1 answer

I had the same crash, you need read and write permissions for the db file and directory.

+13
source

All Articles