[英]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>" . "₦ " . $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>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
}
} else {
$output = "No product yet";
} // return $output;
return $output;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.