我有一個php文件,我試圖從數據庫獲取值並顯示在網頁中。我已經寫了一個foreach循環,但它幾次顯示相同的輸出。這裏是我的代碼輸出重複在foreach循環中php
<?php
session_start();
$host="localhost";
$user="root";
$pass="";
$db="documentation";
$off=$_GET['id'];
$conn=mysqli_connect($host,$user,$pass,$db);
$query=mysqli_query($conn,"SELECT category_name FROM category WHERE id='$off'");
$rslt=mysqli_fetch_assoc($query);
$query1=mysqli_query($conn,"SELECT cat_description FROM category WHERE id='$off'");
$rslt1=mysqli_fetch_assoc($query1);
$shown=mysqli_query($conn,"SELECT b.subcat_name, b.id, b.description
FROM category a, subcategory b
WHERE b.cat_name = (SELECT category_name FROM category WHERE id='$off')");
?>
<html>
<head>
<title>Category</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/mystyle.css">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php
foreach ($shown as $showsubn) { ?>
<div class="row">
<div class="col-md-12">
<p class="border1">
<p class="margin">
<i class="fa fa-cube"></i>
<a href="subcat.php?id=<?php echo $showsubn['id']; ?>"><font size="4"><?php implode(" ",$showsubn); echo $showsubn['subcat_name'];?> </font></a>
<p class="margin"><?php implode(" ",$showsubn); echo $showsubn['description'];?></p>
</p>
</p>
<p class="border4"></p>
</div>
</div>
<?php
}; ?>
</div>
</body>
</html>
輸出應該是 canbangla youbangla
卻是露出 canbangla canbangla canbangla ...高達12分多次 youbangla youbangla youbangla .. .upto 12幾次
了我的解決方案 在我的查詢中使用DISTINCT –