Reputation: 39
i've got a database to work with where articles are saved. Unfortunately and for whatever reason, they stored the picture information in BLOB in the database. Now i need to create folders that contain the picture data - thats perfectly working but when i try to select a greater amount of data, the script fails or runs into errors (gives back false). Can you somehow help me out to query 16.000-20.000 lines and extract it to a file?
Here`s my code
<?php
$dbuser = 'user';
$dbpass = 'pass';
try {
$db = $dbh = new PDO('mysql:host=localhost;dbname=Tventas', $dbuser, $dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::NULL_EMPTY_STRING);
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\
";
}
try {
$sql="SELECT id_imagen, imagen FROM imagen900";
//$stmt = $db->prepare($sql);
//$stmt->execute();
//$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($db->query($sql) as $row){
$filename = $row['id_imagen'].".jpg";
if (!empty($row['imagen'])) {
$blob=$row['imagen'];
if (file_exists($filename)){
echo "<pre>Datei bereits vorhanden: ";
print_r($filename);
echo "</pre>";
}
else
{
file_put_contents($filename,$blob);
echo "<pre>";
print_r($filename);
echo "</pre>";
}
}
}
}
catch (PDOException $e) {
echo"Whoops! Something went wrong!";
echo" Query with error: ".$sql;
echo" Reason given:".$e->getMessage()."\
";
return false;
}
?>
Upvotes: 2
Views: 419
Reputation: 8105
You are trying to load all the 20 000 images into memory.
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
Try looping through the result set instead.
$sql = "SELECT id_imagen, imagen FROM imagen900";
foreach ($db->query($sql) as $row) {
...
}
Upvotes: 3