Home > database >  PHP mysqli loop stops abruptly when downloading images [duplicate]
PHP mysqli loop stops abruptly when downloading images [duplicate]

Time:09-26

I have a mysqli while loop that fetches different URLs which I need to download, however the process stops abruptly after one while loop, the mysqli error given is "mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in ..." for the first "$query = mysqli_query($con, $sql) or die (mysqli_error($con));"

If I simply echo out some text for each while loop, it works fine, so it seems to be some issue with the copying process?

<?php

ini_set("memory_limit", "-1");
set_time_limit(0);

$msg = '';

$sql = "SELECT * FROM images WHERE downloaded='0'";

$query = mysqli_query($con, $sql) or die (mysqli_error($con));

while ($row = mysqli_fetch_array($query)) { 
$id = $row["id"];
$original = $row["original"];
$thumb = $row["thumb"];
$small = $row["small"];
$medium = $row["medium"];
$large = $row["large"];
$large_crop = $row["large_crop"];

$random = randomString(9);

$sql2 = "UPDATE images_properties SET slug='$random' WHERE id='$id'";
$query = mysqli_query($cnx, $sql2) or die (mysqli_error());

if (!file_exists("../uploads/$random")) {
mkdir("../uploads/$random");
} // end make file

if(copy($original, '../uploads/'.$random.'/'.$random.'-o.jpg')){
$copy_original = true;
}
if(copy($thumb, '../uploads/'.$random.'/'.$random.'-t.jpg')){
$copy_thumb = true;
}
if(copy($small, '../uploads/'.$random.'/'.$random.'-s.jpg')){
$copy_small = true;
}
if(copy($medium, '../uploads/'.$random.'/'.$random.'-m.jpg')){
$copy_medium = true;
}
if(copy($large, '../uploads/'.$random.'/'.$random.'-l.jpg')){
$copy_large = true;
}
if(copy($large_crop, '../uploads/'.$random.'/'.$random.'-lc.jpg')){
$copy_large_crop = true;
}

if(($copy_original == true) && ($copy_thumb == true) && ($small == true) && ($copy_medium == true) && ($copy_large == true) && ($copy_large_crop == true)){

$sql3 = "UPDATE images SET downloaded='1' WHERE id='$id'";
$query = mysqli_query($con, $sql3) or die (mysqli_error());

$msg .= $id.'- success - '.$random.'<br/>';

} // end if all are copied

} // end while


echo $msg;

?>

CodePudding user response:

Use different variable names for the results of each mysqli_query() call.

Because the $query variable is reassigned inside the while loop, the loop's condition is based on a different value every time.

  • Related