I'm trying to select data from a mysql LONGBLOB field, but the memory usage seems to grow out of control. I've tried with PDO and mysqli. (no problem inserting the data)
<?php
$path = 'test.txt';
$pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'pass');
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
$stmt = $pdo->prepare("select contents from files where path = :path limit 1");
$stmt->bindValue(':path', $path, PDO::PARAM_STR);
$result = $stmt->execute();
$stmt->bindColumn(1, $data, PDO::PARAM_LOB);
$result = $stmt->fetch(PDO::FETCH_BOUND);
var_dump(strlen($data));
var_dump(memory_get_peak_usage());
The output when running that is:
int(198608116)
int(1902241376)
so the column is 198.61MB, but the scripts memory usage reaches 10 times that 1,902.24MB.
Any idea what I am doing wrong?