显示数据库中的值列表

时间:2016-01-18 19:56:40

标签: php mysqli

$sql   = "SELECT * FROM themes";
$query = mysqli_query($db_conx, $sql);

while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $id           = $row["id"];
    $name         = $row["name"];
    $developers   = $row["developers"];
    $description  = $row["description"];
    $category     = $row["category"];
    $logo         = $row["logo"];
    $officialLink = $row["link"];

    //////////////////////

    $sql          = "SELECT * FROM file WHERE software_id = '$id'";
    $query        = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $version = $row["version"];
        $file    = $row["file"];
        $link    = $row["link"];
        $created = $row["created"];

        $versions .= '<li><a href="../files/' . $file . '"                                target="_blank">
<i class="fa fa-mail-forward">
</i> ' . $version . '</a></li>';
        $logos = "<img src ='../img/logos/{$logo}' >";
    }

    //////////////////////

    $solfList .= '
                <li>
                <h3>' . $name . '</h3>

                </li>
                ';

}

此代码仅显示一个值。我想显示存储在表中的所有值。我有这个代码。但是我无法理解我犯了什么错误。

2 个答案:

答案 0 :(得分:1)

由于在sql中使用相同的变量,您将丢失第一个循环的值。因此,请使用两个不同的查询变量及其结果。

$sql = "SELECT * FROM themes";
$query = mysqli_query($db_conx, $sql); 
            while ($row_outer = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
            $id = $row_outer["id"];
            $name = $row_outer["name"];
            $developers = $row_outer["developers"];
            $description = $row_outer["description"];
            $category = $row_outer["category"];
            $logo = $row_outer["logo"];
            $officialLink = $row_outer["link"];
            //////////////////////
            $solfList = "";
            $sql_inner = "SELECT * FROM file WHERE software_id = '$id'";
            $query_inner = mysqli_query($db_conx, $sql_inner); 
                while ($row_inner = mysqli_fetch_array($query_inner, MYSQLI_ASSOC)){ 
                $version = $row_inner["version"];
                $file = $row_inner["file"];
                $link = $row_inner["link"];
                $created = $row_inner["created"];
                    $versions .='<li><a href="../files/' .$file. '"                                target="_blank">
<i class="fa fa-mail-forward">
</i> ' .$version. '</a></li>';
                    $logos = "<img src ='../img/logos/{$logo}' >";
            }
                    //////////////////////
                $solfList .='
                <li>
                <h3>' .$name. '</h3>

                </li>
                ';

            }

答案 1 :(得分:0)

就像我说的,通过运行内部查询,您将丢失所有外部查询结果集。所以你的代码应该是这样的:

$sql = "SELECT * FROM themes";
$query = mysqli_query($db_conx, $sql); 
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
$id = $row["id"];

// your code

$sql = "SELECT * FROM file WHERE software_id = '$id'";
$result = mysqli_query($db_conx, $sql); 
    while ($r = mysqli_fetch_array($result, MYSQLI_ASSOC)){ 
        $version = $r["version"];

        // your code

    }

    // your code

}