I looked around, but did not seem to find any information about this. I am not sure if this is a problem with my code or a known problem with SQLite databases in memory and PDO.
Basically, after inserting one row into the SQLite database table in memory, I expect a query that does not match the inserted element to return zero rows. However, the following code gives one line (false), but does not have the actual PDO error code.
<?php // Create the DB $dbh = new PDO('sqlite::memory:'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Data we'll be using $name = 'Entry'; // Create DB table $dbh->query(' CREATE TABLE Test ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50) NOT NULL ) '); // Insert data $stmt = $dbh->prepare(' INSERT INTO Test ( name ) VALUES ( :name ) '); $stmt->bindParam(':name', $name, PDO::PARAM_STR, 50); $stmt->execute(); // Check data has actually been inserted $entries = $dbh->query(' SELECT * FROM Test ')->fetchAll(PDO::FETCH_ASSOC); var_dump($entries); // Query DB for non-existent items $stmt = $dbh->prepare(' SELECT * FROM Test WHERE name != :name '); $stmt->bindParam(':name', $name, PDO::PARAM_STR); $stmt->execute(); // How many rows returned echo $stmt->rowCount(); // Actual data returned var_dump($stmt->fetch(PDO::FETCH_ASSOC)); ?>
I managed to get around the problem with some hackers, but it would be nice not to do this:
<?php echo ( (0 == $stmt->rowCount()) || ( (1 == $stmt->rowCount()) && (false === (($row = $stmt->fetch(PDO::FETCH_ASSOC)))) && ('0000' == array_pop($dbh->errorInfo())) ) ) ? 'true' : 'false'; ?>
Can someone help or point out any egregious mistakes I could make?
source share