[英]MySQL query doesn't show all the records correctly
The query am running against my database to get the 3 records order it by Random. 查询正在针对我的数据库运行,以按随机顺序获得3条记录。 The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. 问题在于,有时它显示所有3条记录,有时只显示2、1,有时仅显示空白。 In the database I have around 28 records. 在数据库中,我大约有28条记录。
What I have tried 我尝试过的
This is the query that gets the records LIMIT it by 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();
?>
This is the code that runs and gets all 3 records in a loop. 这是运行并在循环中获取所有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 -->
What am I missing? 我想念什么? Is there any alternative way I can achieve this...thanks! 有没有其他方法可以实现这一目标...谢谢!
It looks to me like you're overwriting your $rows
variable within the inner select. 在我看来,您正在覆盖内部选择中的$rows
变量。
foreach($rows as $row){ // <-- first $rows / $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(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. 从您的应用程序逻辑中断开显示,调试这种事情将不会很困难。 Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug. 此外,您有很多重复的代码,这使得事情变得难以管理以及难以调试。
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id
and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient. 此外,如果您运行一个查询,您将不会遇到此问题: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id
,不仅使您的代码变得更简单,而且双foreach循环问题也消失了,但是您对数据库的访问也会更有效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.