查詢正在針對我的數據庫運行以獲取3條記錄,並按Random排列。問題是,有時它會顯示所有3條記錄,有時它只顯示2,1和其他時間只是空白。在數據庫中我有大約28條記錄。MySQL查詢不會正確顯示所有記錄
我已經試過
- 我試圖不LIMIT - 問題同
- 我也呼應了$ suggested_profile_id發現的所有3條記錄出來。
這個就是被記錄3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
這是一個運行,並得到在循環中的所有3條記錄代碼限制它的查詢。
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<a href="<?php echo $suggested_profile_username; ?>" class="bold welcome-name"><?php echo $suggested_profile_name; ?></a>
<span class="f12 gray">271 Mutual Friends</span>
<a href="#" class="welcome-name">Add as friend</a>
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
我在想什麼?有沒有其他辦法可以實現這一點......謝謝!
檢查,在28個記錄多少記錄有'member_status ='activated'' – krishna
@krishna有22個活動記錄 – user3140617
在你的第一個查詢'DESC LIMIT 3'所以刪除'LIMIT 3' – krishna