1

I have a table called "images" in a mySQL database. There are 7 rows and 2 columns in this table. The columns are: "id" and "image" (BLOB type). I want to echo all images from this table as an array.

This is the code I tried to use:

<?php
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx");
if(mysqli_connect_error()) {
    die ("Error message");
}
mysqli_query($link, $query);
$query = "SELECT image FROM images";
$i = 1;
if ($result = mysqli_query($link, $query)) {
    while ($row = mysqli_fetch_array($result)) {
        ${"image".$i} = $row['image'];
        $i++;
    }
    $array = array($image1, $image2, $image3, $image4, $image5, $image6, $image7);
    echo json_encode($array);
}
?>

I'm getting a blank page when I run this code.

I really appreciate if you could give me any ideas of how I can fix this.

Just to explain why I need to echo these images as an array: I'm going to use AJAX in a JS file to put these images in specific divs in another HTML file.

2 Answers2

0
<?php
ini_set('display_errors', 1);
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx");
if(mysqli_connect_error()) {
    die ("Error message");
}
$query = "SELECT image FROM images";
mysqli_query($link, $query);
$i = 1;
if ($result = mysqli_query($link, $query)) {
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $aImages[] = $row['image'];
    }

    echo json_encode($aImages);
}
?>
mohe14
  • 47
  • 6
  • Thank you for trying to help, but I'm still getting a blank page – Guilherme Duque Apr 03 '17 at 16:56
  • Can you add this `ini_set('display_errors', 1);` on top page after ` – mohe14 Apr 03 '17 at 16:59
  • I added `ini_set('display_errors', 1);` and I got "Warning: mysqli_query(): Empty query in 'php file address' on line 7". Sorry. I'm a beginner in programming. What is snippet code? I tried to find out what it is on the web but I didn't understand. If I use the original code I posted, when i print the array, I get the images as enormous amounts of characters – Guilherme Duque Apr 03 '17 at 17:07
  • you call mysqli_query before assign $query – mohe14 Apr 03 '17 at 17:10
  • Yes. You're right. Now, I removed `mysqli_query($link, $query);` and it doesn't show any other warning messages. But I still don't get the images – Guilherme Duque Apr 03 '17 at 17:14
  • Now I copied your code again and I used it but I still get a blank page – Guilherme Duque Apr 03 '17 at 17:22
0

I would first try to print_r your array to see if it even exists. http://php.net/manual/en/function.print-r.php

After that if there is output of an empty array, you can always just push each image into your array in the while loop. Therefore not limiting yourself to a set number of images. http://php.net/manual/en/function.array-push.php

Another step you can take to help reduce or atleast debug errors in the future is to turn on debugging. https://stackoverflow.com/a/21429652

Community
  • 1
  • 1
Kirby
  • 1
  • 1
  • Thank you for the tips. I printed my array and I get enormous amounts of characters for each array value. So it exists, but it doesn't show the images. – Guilherme Duque Apr 03 '17 at 17:09