php - PDO Sqlite `fetchAll()` exhausting all memory -


i have following php script use pdo connect sqlite3 database , count records:

<?php  ini_set('max_execution_time', 300); error_reporting(e_all); ini_set("display_errors", 1);  $db = new pdo('sqlite:db/mydb.sl3'); $result = $db->query('select * mytable'); $rows = $result->fetchall(); $row_count = count($rows);  echo "rows: " . $row_count . "\n";  ?> 

all if run shell (php-cli):

rows: 175412

but running web throws me following error:

fatal error: allowed memory size of 134217728 bytes exhausted (tried allocate 14 bytes) in /var/www/test/sqlitepdo.php on line 9

setting memory in php not solution because should not happen, because doesn't happen mysql same results example.

any idea?

so creating array 175k rows, sake of counting number of rows. bound create memory issues. if manage resolve pointless that.

for insight, 134217728 bytes ~ 134 mb. you're reading more 134 mb worth of data per call of query in issue if manage suppress error.

you can change query ask sqllite what's row count.

select count(*) c mytable 

then have 1 row row count, use that.

$row = $result->fetch(pdo::fetch_assoc); echo "rows: " . $row["c"] . "\n"; 

this can answer why not generating error in cli mode


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -