我有一个mysql查询和我看起来像这样的html代码。
<?php
/////////////////////////////////////////////////////////////
// RANDOM USER DISPLAYED WHICH MATCHS
/////////////////////////////////////////////////////////////
$statement = $dbConn->prepare("SELECT user_name, profile_image_url, user_age, country FROM users WHERE profile_image = '2' AND gender = ? ORDER BY RAND() LIMIT 2;");
$statement->execute(array($logged_in_user['searching_for']));
$random_match= $statement->fetch(PDO::FETCH_BOTH);
/////////////////////////////////////////////////////////////
// END OF RANDOM QUESTIONS
/////////////////////////////////////////////////////////////
?>
<div id="front_match">
<div class="front_box">
<div class="front_box_left">
<div style="position: relative;">
<img src="images/woman_1.jpg" style="max-width:100%;height:auto;">
<div class="transparent">Information about user one comes here</div>
</div>
</div>
<div class="front_box_right">
<div style="position: relative;">
<img src="images/woman_2.jpg" style="max-width:100%;height:auto;">
<div class="transparent">Information about user two comes here</div>
</div>
</div>
</div> <!-- div box finished here -->
</div> <!-- div match finished here -->
如何插入从数据库收到的信息 &#34;关于用户1的信息&#34;然后&#34;关于用户2的信息&#34;? 您可以看到我使用限制2,所以我想在一个地方显示1个结果,在第二个地方显示第二个结果
Cheerz
答案 0 :(得分:1)
您仍然可以使用fetch_array
来实现目标。将其存储在一个数组中,然后您可以随时调用它。
while($statement->fetch()){
$user_name_arr[] = $user_name;
$user_age_arr[] = $user_age;
/* ... REST OF CODE ... */
}
然后手动调用它们:
<div id="front_match">
<div class="front_box">
<div class="front_box_left">
<div style="position: relative;">
<img src="images/woman_1.jpg" style="max-width:100%;height:auto;">
<div class="transparent">
<?php
echo "Name: ".$user_name_arr[0];
echo "Age: ".$user_age_arr[0];
/* ECHO REST OF THE INFORMATION */
?>
</div>
</div>
</div>
<div class="front_box_right">
<div style="position: relative;">
<img src="images/woman_2.jpg" style="max-width:100%;height:auto;">
<div class="transparent">
<?php
echo "Name: ".$user_name_arr[1];
echo "Age: ".$user_age_arr[1];
/* ECHO REST OF THE INFORMATION */
?>
</div>
</div>
</div>
</div> <!-- div box finished here -->
</div> <!-- div match finished here -->
我只是想知道,为什么你要分开它们而不是使用while循环。接下来就是这样:
<div id="front_match">
<div class="front_box">
<?php
while($statement->fetch()){
?>
<div class="front_box_left">
<div style="position: relative;">
<img src="<?php echo $profile_image_url; ?>" style="max-width:100%;height:auto;">
<div class="transparent">
<?php
echo "Name: ".$user_name;
echo "Age: ".$user_age;
/* ECHO REST OF THE INFORMATION */
?>
</div>
</div>
</div>
<?php
} /* END OF WHILE LOOP */
?>
</div> <!-- div box finished here -->
</div> <!-- div match finished here -->
答案 1 :(得分:0)
你想要两排吗?然后取两次:
$first_match = $statement->fetch(PDO::FETCH_BOTH);
$second_match = $statement->fetch(PDO::FETCH_BOTH);
您可以将$first_match
用于第一个表格,将$secon_match
用于第二个表格。