简体   繁体   English

将图像从数据库显示到PHP页面

[英]Display images from a database into a PHP page

Im wondering what can I do to make images from a database on Php display on my page. 我想知道如何做才能从页面上显示的Php数据库中获取图像。 This is what I have 这就是我所拥有的

images .php 图片.php

$query = "SELECT * FROM images ORDER BY name ASC ";

            $result = $db->query($query);
            $num_result = $result->num_rows;

            echo "<h1> Images</h1>";

            for ($i = 0; $i < $num_result; $i++){

                $row = $result->fetch_assoc();
                $name = $row['name'];
                $URL = $row['imageURL'];


                $array = array($URL);
            }

foreach ($array as $image){
        echo '<tr>';
        echo '<td><img class="coupons" src="'.$image.'"/></td>';
        echo '<td></td>';
        echo '</tr>';
        echo '<tr>';

          }  

This is just printing only one image and I have 10 in my database, what can I do or change to print all of the images from the database? 这仅打印一张图像,而我的数据库中只有10张图像,我该怎么做或更改以打印数据库中的所有图像? Thanks 谢谢

You should change 你应该改变

$array = array($URL);

into 进入

$array[] = $URL;

And add before line: 并在行之前添加:

for ($i = 0; $i < $num_result; $i++){

add line: 添加行:

$array = array();

Try this, 尝试这个,

$array = array();//initialize here 
for ($i = 0; $i < $num_result; $i++){
            $row = $result->fetch_assoc();
            $name = $row['name'];
            $URL = $row['imageURL'];
            $array[] = $URL;
    }

You can rewrite your code as, 您可以将代码重写为

    while ($row = $result->fetch_assoc()){
        $name = $row['name'];
        $URL = $row['imageURL'];    
        echo '<tr>';
        echo '<td><img class="coupons" src="'.$URL.'"/></td>';
        echo '<td></td>';
        echo '</tr>';
        echo '<tr>';
    }
$query = "SELECT * FROM images ORDER BY name ASC ";

$result = $db->query($query);
$num_result = $result->num_rows;

$array = array();

echo "<h1> Images</h1>";

for ($i = 0; $i < $num_result; $i++){

    $row = $result->fetch_assoc();
    $name = $row['name'];
    $URL = $row['imageURL'];


    $array[] = URL;
}

foreach ($array as $image){
    echo '<tr>';
    echo '<td><img class="coupons" src="'.$image.'"/></td>';
    echo '<td></td>';
    echo '</tr>';
}

You also had a trailing <tr> which may cause styling issues 您也有一个尾随<tr>可能会导致样式问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM