两个sql查询在一个地方

时间:2010-05-20 12:01:03

标签: php sql

        <?php
    $results = mysql_query("SELECT * FROM ".TBL_SUB_RESULTS."
                           WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user =   
                           '$_SESSION[username]'  ") ;
    $num_rows = mysql_num_rows($results);
        if ($num_rows > 0)
        { 
            while( $row = mysql_fetch_assoc($results)) 
            {
                extract($row);
                $q = mysql_query("SELECT name FROM ".TBL_FRIENDLY." WHERE id = '$ccompid'");
                while( $row = mysql_fetch_assoc($q)) 
                {
                    extract($row);
                ?>
                    <table cellspacing="10" style='border: 1px dotted' width="300" bgcolor="#eeeeee">
                    <tr>
                    <td><b><? echo $name; ?></b></td>
                    </tr><tr>
                    <td width="100"><? echo $home_user; ?></td>
                    <td width="50"><? echo $home_score; ?></td>
                    <td>-</td>
                    <td width="50"><? echo $away_score; ?></td>
                    <td width="100"><? echo $away_user; ?></td>
                    </tr><tr>
                    <td colspan="2"><A HREF="confirmresult.php?fixid=<? echo $fix_id; ?>">Accept / Decline</a></td>
                    </tr></table><br>
                    <?
                }


            }


        }
        else
        {
            echo "<b>You currently have no results awaiting confirmation</b>";
        }

    ?> 

我正在尝试运行两个查询,如您所见。 但它们都不起作用。 有没有更好的方法来构建这个,我有一个大脑冻结! 感谢

顺便说一下,我的SQL不会保持这种形式! 之后我会保护它

3 个答案:

答案 0 :(得分:0)

SELECT  *
FROM    TBL_SUB_RESULTS ts
LEFT JOIN
        TBL_FRIENDLY tf
ON      tf.id = ts.ccompid
WHERE   ts.user_submitted != '$_SESSION[username]'
        AND ts.home_user = '$_SESSION[username]' OR ts.away_user = '$_SESSION[username]'

答案 1 :(得分:0)

出于调试目的,请尝试将查询打印到屏幕上。检查您的最终查询中是否发现任何错误。你可能只是拼错了一个变量名。

<?php
$query = "SELECT * FROM ".TBL_SUB_RESULTS." WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user = '$_SESSION[username]'";
echo "query: ".$query;

$results = mysql_query($query);
// rest of code...

答案 2 :(得分:0)

我认为你的WHERE条款需要括号:

WHERE user_submitted != '$_SESSION[username]' 
    AND (home_user = '$_SESSION[username]' 
        OR away_user = '$_SESSION[username]')