简体   繁体   English

MYSQL SELECT 仅查询一个数据

[英]MYSQL SELECT QUERY ONLY ONE DATA

view_products() below displays only one data from my table product which contains 20 data.下面的view_products()仅显示我的表product中的一个数据,其中包含 20 个数据。 Depending on where you place return $output;根据您放置的位置return $output; either inside while loop which displays the first data or outside the while loop which displays the last data.在显示第一个数据的while loop while loop或在显示最后一个数据的 while 循环外。

<?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;
}

The reason is that you are resetting the content of $output in the first line of your loop $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";原因是您在循环的第一行重置$output的内容$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">"; So if you put the return at the end of the loop, in the first loop it will return the first record and exit the function, if you put it at the end of the function, in each loop $output will be cleared and the content of that loop will only be written in $output so at the end of the function you will only have the content of the last loop in $output所以如果你把return放在循环的末尾,在第一个循环中它将返回第一条记录并退出function,如果你把它放在function的末尾,在每个循环中$output都会被清除,内容该循环的内容将仅写入$output因此在 function 的末尾,您将只有最后一个循环的内容$output

What you can to is to set $output to an empty string and then just append everything in your loop.您可以将$output设置为空字符串,然后将 append 设置为循环中的所有内容。 Also set the value of $output in the else block and then at the end return $output还要在else块中设置$output的值,然后在最后返回$output

function view_products()
{
global $dbconn;

    $sql = "SELECT * FROM product";
    $result = mysqli_query($dbconn, $sql);

    
    if (mysqli_num_rows($result) > 0) {
        $output = "";
        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>";
        }
    } else {
        $output = "No product yet";
    } // return $output;
    return $output;
}

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

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