如何回显PHP mySQL查询中的图像数组?

时间:2017-04-03 16:38:59

标签: php mysql arrays blob

我有一张名为" images"在mySQL数据库中。此表中有7行和2列。列是:" id"和"图像" (BLOB类型)。我希望将此表中的所有图像作为数组回显。

这是我尝试使用的代码:

<?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);
}
?>

我在运行此代码时收到空白页。

如果你能告诉我如何解决这个问题,我真的很感激。

解释为什么我需要将这些图像作为数组回显:我将在JS文件中使用AJAX将这些图像放在另一个HTML文件中的特定div中。

2 个答案:

答案 0 :(得分: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);
}
?>

答案 1 :(得分:0)

我会首先尝试print_r你的数组,看看它是否存在。 http://php.net/manual/en/function.print-r.php

之后如果有一个空数组的输出,你总是可以在while循环中将每个图像推入你的数组。因此,不限制自己使用一定数量的图像。 http://php.net/manual/en/function.array-push.php

您可以采取的另一个步骤是打开调试,以帮助减少或至少在将来发生调试错误。 https://stackoverflow.com/a/21429652