声明中的循环问题

时间:2010-12-02 16:49:23

标签: php mysql tags

我希望有人可以帮忙尖叫!

基本上我试图用下面的陈述做一些事情;

首先,我想检查member_categories_position中是否存在用户ID。 如果我想要那么从第二个语句中排除所有条目,其中member_id等于第一个语句的所有结果 第三个语句是如果member_id不存在于member_categories位置则显示的else语句。

问题 - 第一个系统的结果很好地循环,但是当我尝试插入第二个语句(!='$ memid')时,没有产生任何结果并且没有效果。我认为问题是$ memid是一个循环结果。

我如何得到第二个声明,表示member_categories_position中的任何member_id都不会显示在该声明中?

$sql2 = "
    SELECT * 
    FROM member_categories_position a 
    JOIN member_users b 
        ON b.id = a.member_id";

$rs2 = mysql_query($sql2);
while ($row = mysql_fetch_array($rs2))
{
    $memid = "".$row['member_id']."";   
}

if(mysql_num_rows($rs2) != 0)
{
    $new= "
        SELECT * 
        FROM member_categories 
        JOIN member_users 
            ON member_categories.member_id=member_users.id 
        JOIN member_config 
            ON member_categories.member_id=member_config.member_id 
        WHERE 
            member_categories.categories='$category' 
            AND member_categories.member_id !='$field'  
        GROUP BY member_config.member_id  
        ORDER BY RAND() limit 0,42";

    $rs = mysql_query($new);

    while ($row = mysql_fetch_assoc($rs)) 
    {
        echo "result excluding member ids from the first statement";
    }
    echo "<div class=\"clear\"></div>";
}
else
{

    $new= "
        SELECT * 
        FROM member_categories 
        JOIN member_users 
            ON member_categories.member_id=member_users.id 
        JOIN member_config 
            ON member_categories.member_id=member_config.member_id 
        WHERE
            member_categories.categories='$category' 
        GROUP BY member_config.member_id  
        ORDER BY RAND() limit 0,42";

    $rs = mysql_query($new);

    while ($row = mysql_fetch_assoc($rs)) 
    {
        echo "Result with all member ids";
    }
    echo "<div class=\"clear\"></div>";

}  } <-- (second is a stray from original post)

1 个答案:

答案 0 :(得分:0)

$ memid不在范围内,因为它似乎是在循环内定义的。尝试定义$ memid ='';在你的脚本的顶部..像这样。

$memid =  '';
$sql2 = "
         SELECT * 

这样就可以在下面使用它时定义..