PHP mysqli从不同的表中提取数据并使用2 while函数进行回显 [英] PHP mysqli extract data from different tables and echo with 2 while functions
问题描述
我坚持对网站的一部分进行编码.我正在从第一张表中提取您正在使用的帐户的友谊.
I'm stuck coding a part of a website. I'm extracting from the first table the friendship for the account you are using.
然后我创建一个while
以获取朋友的所有用户名,然后查询个人资料表以获取其他两个信息,例如性别和出生日期.
Then I make a while
to get all usernames of my friends then I query the profiles table to take out other two information like sex and birth date.
我的问题是第一个if(query)
和while(friend_username)
可以正常工作,我可以取出所有数据并打印用户列表,但是当我使用第二个while(sex,birth_date)
进行第二个查询时,没有任何效果,我不能找到问题了,这是我尝试修复的第二天,但找不到解决方案.
My problem is that the first if(query)
and while(friend_username)
works fine and I can take out all the data and print the list of users but when I do the second query with the second while(sex,birth_date)
nothing works and I cant find the problem it's the second day I try to fix it but I can't find a solution..
很抱歉我的英语不好,也很抱歉我的编码能力很差.
Sorry for my bad English and also sorry for my bad coding skills.
我不知道我在做什么错.
I don't know what I'm doing wrong.
<?php
$query = "SELECT friend_username FROM friends WHERE username = ? AND amicizia = '1' AND ban = '0' ";
if($stmt = $mysqli->prepare($query))
{
// bind parameter
$stmt->bind_param('s', $username );
// execute
$stmt->execute();
// bind result to a variable
$stmt->bind_result($amici);
// iterate through the results
echo "$amici";
while ($stmt->fetch())
{
$queryu = "SELECT sesso , data_nascita FROM profiles WHERE username = ? ";
if($stmtp = $mysqli->prepare($queryu))
{
// bind parameter
$stmtp->bind_param('s', $amici );
// execute
$stmtp->execute();
// bind result to a variable
$stmtp->bind_result($sesso, $data_nascita);
while ($stmtp->fetch())
{
// FARE CICLO WHILE CHE ESTRAPOLA LE INFORMAZIONE DALLA TABELLA PROFILES MA SISTEMARE PRIMA HE AL LOGIN COMPILI I CAMPI SULLA TAB PROFILES
echo "
<tr>
<td>
<div class=\"gallery\" style=\"height: 76px;\">
<a href=\"img/profilo.jpg\"><img class=\"align-left\" src=\"img/amico.png\" width=\"60\" height=\"60\" /></a>
</div>
</td>
<td>
<ul style=\"height:45px;\">
<li style=\"padding: 0;\">$amici</li>
<li style=\"padding: 0;\">$sesso</li>
<li style=\"padding: 0;\">$data_nascita</li>
</ul>
</td>
<td>
<p>Lorem Ipsum è un testo segnaposto utilizzato nel settore della tipografia e della stampa. Lorem Ipsum è considerato il testo segnaposto standard sin dal sedicesimo secolo</p>
</td>
<td>
<a href=\"\"><i class=\"icon-user icon-2x\"></i></a>
<a href=\"\"><i class=\"icon-envelope icon-2x\"></i></a>
<a href=\"\"><i class=\"icon-heart-empty icon-2x\"></i></a>
<a href=\"\"><i class=\"icon-trash icon-2x\"></i></a>
</td>
</tr>
";
}
}
}
}
?>
推荐答案
如果可能,应该使用表联接在一个查询中获取所有必需的数据. 使用代码段中的表格,它看起来可能像这样:
If possible, you should use a table join to fetch all required data in one query. Using the tables from your code snippet it could look like this:
SELECT a.friend_username, b.sesso, b.data_nascita FROM friends as a
JOIN profiles as b ON(b.username=a.friend_username)
WHERE a.username = ? AND a.amicizia = '1' AND a.ban = '0'
要回答您的实际问题,可以执行嵌套查询,但要使您的示例正常工作,您将需要创建第二个mysql连接(例如:$ mysqli2 = new mysqli(...)),并在其中使用外部的while循环.
To answer your actual question, it is possible to do nested queries but for your example to work you would need to create a second mysql connection (ex: $mysqli2 = new mysqli(...)), and use that in the outer while loop.
这篇关于PHP mysqli从不同的表中提取数据并使用2 while函数进行回显的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!