we currently encounter a strange bug using PDO and an SQLite in memory database.
Here is a peace of code to reproduce the problem.
Execute the following PHP code
<?php
// Gets a PDO reference with in memory SQLite (i.e using the 'sqlite::memory:' dsn)
$pdo = new \PDO('sqlite::memory:', 'sa', '');
$pdo->exec(
'CREATE TABLE IF NOT EXISTS scheduled_task' .
'(' .
'id INTEGER NOT NULL,' .
'date_and_time INTEGER NOT NULL,' .
'task_class VARCHAR(256) NOT NULL,' .
'task_parameters VARCHAR(1024) NOT NULL' .
')'
);
// Just a statement to count rows in our table
$pdoStatementCount = $pdo->prepare('select count(*) from scheduled_task');
// At first ensure our table is empty
$pdoStatementCount->execute();
var_dump('At beginning table has size \'' . intval($pdoStatementCount->fetchColumn()) . '\'.');
// Insert 2 rows inside our table
$pdoStatementInsert = $pdo->prepare(
'insert into scheduled_task(id, task_class, date_and_time, task_parameters) values(?,?,?,?)'
);
$pdoStatementInsert->execute([1, 'tk1', '2015-01-01', '{}']);
$pdoStatementInsert->execute([2, 'tk2', '2015-01-02', '{}']);
// Ensure the 2 rows were inserted
$pdoStatementCount->execute();
var_dump('Then table has size \'' . intval($pdoStatementCount->fetchColumn()) . '\'.');
// Now create a statement to select only the first row
$pdoStatementSelect1 = $pdo->query('select * from scheduled_task where date_and_time < \'2015-01-02\'');
// Here it seems their is a bug with the Iterator associated to the PDO Statement
foreach($pdoStatementSelect1 as $row) {
var_dump($row['id']);
// Inserts a new row inside our table
$pdoStatementInsert = $pdo->prepare(
'insert into scheduled_task(id, task_class, date_and_time, task_parameters) values(?,?,?,?)'
);
if($pdoStatementInsert->execute([3, 'tk3', '2015-01-01', '{}']) === false) {
var_dump('Fail inserting row in loop !');
var_dump($pdoStatementInsert->errorCode());
var_dump($pdoStatementInsert->errorInfo());
}
// Deletes the first row
$pdo->query('delete from scheduled_task where id = ' . $row['id']);
}
// This fails with SQLite, we should have 2 rows inside our table here
$pdoStatementCount->execute();
var_dump('At the end table has size \'' . intval($pdoStatementCount->fetchColumn()) . '\'.');
The output of this test is the following.
string(32) "At beginning table has size '0'."
string(24) "Then table has size '2'."
string(1) "1"
string(1) "3"
string(30) "At the end table has size '1'."
Conclusion, bug in PDO SQLite ?
Please note that our foreach loop is executed 2 times instead of one. So it seems the PDOStatement object used in our loop is updated (don't know how) because we inserted a new row inside our loop.
If we change our PDO instance to use MySQL then it works, so I think this bug is related to SQLite only.
Although if I change the insert instruction placed inside the loop to this (i.e the date does not match with our select) then it works.
$pdoStatementInsert->execute([3, 'tk3', '2015-01-02', '{}'])
So its like of the SQLite PDOStatement were doing a new select query transparently...
Do you know why we encounter this behavior ? Do you think its normal ? If not do we have to report it at https://bugs.php.ne ?
FINAL ANSWER - THIS IS NOT A BUG See https://bugs.php.net/bug.php?id=72267&edit=2 Thanks for your help !
Thanks.
$results = $stmt->fetchAll(); foreach ($results as $row) { ...
? That way you iterate on a fixed array, not a variable resultset.