猿问

MYSQL SELECT 查询仅一条数据

view_products()下面仅显示我的表中的一项数据,product该表包含 20 条数据。取决于您放置在显示第一个数据的return $output;内部或显示最后一个数据的外部的位置。while loopwhile loop


<?php echo view_products(); ?>

$dbhost = "localhost";

$dbuser = "root";

$dbpass = "password@edadmin";

$dbname = "estore";

$dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);


//Test if connection occurred,

if (mysqli_connect_errno()) {

    die("Database connection failed: " .

        mysqli_connect_error() .

        " (" . mysqli_connect_errno() . ")");

}

function view_products()

{

    global $dbconn;


    $sql = "SELECT * FROM product";

    $result = mysqli_query($dbconn, $sql);


    

    if (mysqli_num_rows($result) > 0) {

        while ($products = mysqli_fetch_assoc($result)) {

            $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";

            $output .= "<div class=\"portfolio-wrap\"><figure>";

            $output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";

            $output .= "<a href="  . $products['ProductImage'] . " data-lightbox=\"portfolio\" data-title=\"App 1\" class=\"link-preview\" title=\"Preview\"><i class=\"ion ion-eye\"></i></a>";

            $output .= "</figure>";

            $output .= " <div class=\"portfolio-info\">";

            $output .= "<p><a href=\"#\">" . $products['ProductName'] . " </a></p>";

            $output .= "<p>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";

            $output .= "</div></div></div>";

            return $output;

        }

    } else {

        return "No product yet";

    } // return $output;

}


饮歌长啸
浏览 209回答 1
1回答

呼如林

原因是您正在重置$output循环第一行中$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">"; 的内容因此,如果您将 return 放在循环末尾,则在第一个循环中它将返回第一条记录并退出该函数,如果您将其放在函数的末尾,在每个循环$output中将被清除,并且该循环的内容将仅被写入,$output因此在函数的末尾,您将只拥有最后一个循环的内容$output您可以设置$output为空字符串,然后将所有内容附加到循环中。$output还要在块中设置 的值else,然后在最后返回$outputfunction view_products(){global $dbconn;&nbsp; &nbsp; $sql = "SELECT * FROM product";&nbsp; &nbsp; $result = mysqli_query($dbconn, $sql);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (mysqli_num_rows($result) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; $output = "";&nbsp; &nbsp; &nbsp; &nbsp; while ($products = mysqli_fetch_assoc($result)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<div class=\"portfolio-wrap\"><figure>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<a href="&nbsp; . $products['ProductImage'] . " data-lightbox=\"portfolio\" data-title=\"App 1\" class=\"link-preview\" title=\"Preview\"><i class=\"ion ion-eye\"></i></a>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "</figure>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= " <div class=\"portfolio-info\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<p><a href=\"#\">" . $products['ProductName'] . " </a></p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<p>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "</div></div></div>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $output = "No product yet";&nbsp; &nbsp; } // return $output;&nbsp; &nbsp; return $output;}
随时随地看视频慕课网APP
我要回答