在我的本地xampp上运行while脚本时,它只是一遍又一遍地返回相同的结果,这是我正在使用的PHP:
<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
$getids = mysql_fetch_assoc($getmembers);
while($id = $getids['userid']){
$getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
echo ($getusers['username']);
echo('<br />');
}
?>
我执行的所有操作都是:http://prntscr.com/12nh6g我的数据库包含4个注册到该组的不同用户。我不确定在MySQL中加入表格,因为我不知道该怎么做。
如果我加一个休息时间;然后它只返回一个结果,而不是超时。
非常感谢任何帮助!
此外,我很抱歉PHP是多么混乱,我是新手。
答案 0 :(得分:1)
while($id = $getids['userid']){ #This condition will always be true.
在下面的循环中使用您的mysql_fetch_assoc
语句。
while ($getids = mysql_fetch_assoc($getmembers)) {
$id = $getids['userid']
$getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
echo $getusers['username'];
echo '<br />';
}
对于子查询,您可以尝试
SELECT look, username FROM users
WHERE id IN
(SELECT userId FROM group_memberships
WHERE groupid = $id
LIMIT 10)
使用加入,
SELECT u.look,u.username
FROM users as u
LEFT JOIN group_memberships as gm
on gm.userId = u.id
WHERE gm.groupid = $id
LIMIT 10
注意: Please, don't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
答案 1 :(得分:1)
<?php
$id = (int) $_GET['id']; // very important part here, I cast the id to int, else you have a SQL injection leak
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
while ($membership = mysql_fetch_assoc($getmembers)) // on each pass we take a new result off first query
{
$user = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '".$membership['user_id']."'"));
echo ($user['username']);
echo('<br />');
}
?>
答案 2 :(得分:1)
在您的代码中:
<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
$getids = mysql_fetch_assoc($getmembers);
while($id = $getids['userid']){
$getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
echo ($getusers['username']);
echo('<br />');
}
?>
告诉PHP “做循环”,而$ id被分配给$ getids ['userid']的值
因此,您将拥有一个无限循环,因为$ id一直被分配(并且始终具有相同的值)$ getids永远不会在while循环中更改。
但即使$ getids即将改变它仍然会处于无限循环中,因为循环不检查$ getids ['userid'] 的$ id是否值,它只是分配值。
答案 3 :(得分:1)
<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
while($getids = mysql_fetch_assoc($getmembers)){
$getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
echo ($getusers['username']);
echo('<br />');
}
?>
你的while循环错了,你需要在循环中进行内移,所以直到取出停止。