无法从MySQL PHP显示两个图像链接。

5
function find_image_by_id() {
    global $connection;
    $query = "SELECT * ";
    $query .= "FROM images ";
    $query .= "WHERE page_id={$_GET["page"]}";
    $image_set = mysqli_query($connection, $query);
    confirm_query($image_set);
    return $image_set;
}

function display_image_by_id(){
    $current_image = find_image_by_id();
    while($image=mysqli_fetch_assoc($current_image)){
        $output = "<div class=\"images\">";
        $output .= "<img src=\"images/";
        $output .= $image["ilink"];
        $output .= "\" width=\"72\" height=\"72\" />";
        $output .= $image["phone_name"];
        $output .= "</div><br />";
    }
    mysqli_free_result($current_image);
    return $output;
}

这是我用来显示存储在MySQL中的链接的图像的代码,而这些图像则存储在一个文件夹中。但是,在执行此代码后只会显示第二个值。我想要两个值/图像都被显示。

1个回答

2

试着像这样做 -

你需要做的就是在循环外部初始化这个变量。

 $output =''; //initialize before

所以你的函数看起来像这样 -

function display_image_by_id(){
    $current_image = find_image_by_id();
    $output =''; //initialize before
    while($image=mysqli_fetch_assoc($current_image)){
        $output .= "<div class=\"images\">";
        $output .= "<img src=\"images/";
        $output .= $image["ilink"];
        $output .= "\" width=\"72\" height=\"72\" />";
        $output .= $image["phone_name"];
        $output .= "</div><br />";
    }
    mysqli_free_result($current_image);
    return $output;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接