Monolica
Monolica

Reputation: 115

Sqlite shows an error when it is executed

I am writing a SQLite simple database table entry code. When it is executed it displays an error. Can someone see what and where I am getting wrong?

  <?php 
$db = new PDO("sqlite:waterlevel");
$db->query('create table waterlevel(time integer, ip text, waterlevel text, station text, humidity text, temperature text)');
$in=$db->prepare('insert into waterlevel(time, ip, waterlevel, station, humidity, temperature) values(?, ?, ?, ?, ?, ?)');
$in->execute(array(time(), $_SERVER['REMOTE_ADDR'], $_GET[waterlevel], $_GET[station], $_GET[humidity], $_GET[temperature]));

print_r($db->errorInfo());
?>

Error: Fatal error: Uncaught Error: Call to a member function execute() on boolean in /html/waterlevel.php:5 Stack trace: #0 {main} thrown in /html/waterlevel.php on line 5

Upvotes: 0

Views: 218

Answers (1)

KIKO Software
KIKO Software

Reputation: 16688

You're calling execute() on $in which is supposed to contain a prepared insert statement. The error says it doesn't, it's a boolean. If you look at the return value of PDO::prepare() in the manual, it says:

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

So there's something wrong with the insert statement. Normally I would then put the print_r($db->errorInfo()); directly after that call, to see what the error is. Could you try that, and see what error you get there?

At first sight I don't see anything wrong with the insert statement itself. It could be that SQLite doesn't like the use of time, because there's a function with that name, but I'm not sure. I really cannot guess which error will appear on your server.

Upvotes: 1

Related Questions