2017-04-03 32 views
1

我有一个名为“图像”在mySQL数据库中的表。该表中有7行2列。这些列是:“id”和“image”(BLOB类型)。我想以该数组的形式回显此表中的所有图像。如何从PHP mySQL查询回显图像数组?

这是我尝试使用代码:

<?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中。

回答

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

谢谢你想帮忙,但我仍然得到一个空白页 –

+0

您可以添加此'的ini_set(“display_errors设置”,1);?'首页后' mohe14

+0

@GuilhermeDuque使用代码段代码。 – mohe14

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

+0

谢谢你的提示。我打印我的数组,并为每个数组值获得大量的字符。所以它存在,但它不显示图像。 –