选择查询无法处理链接表(mysql)

时间:2011-04-24 09:49:41

标签: php mysql html

 $sql=mysql_query("SELECT b.book_id,u.user_id,user_name,book_name,review_date,book_rating,review
                  FROM tbl_books b,tbl_user u,tbl_book_reviews br
                  WHERE b.book_id=br.book_id 
                  AND u.user_id=br.user_id
                  AND moderated='n'
                  ORDER BY review_date ASC
                  LIMIT 1");

我想知道上面的select查询的结构是否错误? 因为它为tbl_books.book_id和tbl_user.user_id返回空。但是我得到了其他字段的正确值。

以下是审核书评的完整代码:

    <?php
session_start();

if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_name']) || !isset($_SESSION['password'])) {
    echo "Your session has timed out.";
    exit();}
$errormsg="";
include "scripts/connect_to_mysql.php";
?>
<html>

<head>

<link rel="stylesheet" type="text/css" href="reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="style1.css" media="screen" />
<title>Moderate Reviews</title>

<body>

<div id="layout_wrapper">
<div id="layout_container">
<div id="layout_content">

    <div id="site_title">
        <h1><a href="home.php"></a></h1>
        <h2></h2>
    </div>

    <?php include "header.php";?>

<?php include "navi_admin.php";?>

        <div class="clearer">&nbsp;</div>

    <div id="main">

        <div class="post">

            <div class="post_top">
                <div class="post_title"><h2>Moderate Reviews</h2></div>
            </div>

            <div class="post_body">



    <?php
            if (isset($_POST['mod_save']))
    {
    $b_id=mysql_real_escape_string($_POST['book']);
    $u_id=mysql_real_escape_string($_POST['user']);
       if ($_POST['mod_action']=="a"){
         $sql=mysql_query("UPDATE tbl_book_reviews SET moderated='a' WHERE moderated='n' WHERE book_id='$bid' AND user_id='$uid'");
         $errormsg="Your moderation action has been successfully saved.  You may now moderate the next review below.";
         }
       else if($_POST['mod_action']=="r")
       {
          $sql=mysql_query("UPDATE tbl_book_reviews SET moderated='r' WHERE book_id='$bid' AND user_id='$uid' AND moderated='n'");
         $errormsg="Your moderation action has been successfully saved.  You may now moderate the next review below.";
       }
       else
       {
         $errormsg="Please select a moderation action.";
       }
    }
?>  
    <?php
 $sql=mysql_query("SELECT b.book_id,u.user_id,user_name,book_name,review_date,book_rating,review
                  FROM tbl_books b,tbl_user u,tbl_book_reviews br
                  WHERE b.book_id=br.book_id 
                  AND u.user_id=br.user_id
                  AND moderated='n'
                  ORDER BY review_date ASC
                  LIMIT 1");
 $Rs=mysql_num_rows($sql);
 if($Rs<1)
     {
        $errormsg="There is no new book reviews that need moderation.";
     }
else
  {
       while ($row=mysql_fetch_array($sql))
       {
       $bid=$row['b.book_id'];
       $bname=$row['book_name'];       
       $review_author=$row["user_name"];
       $uid=$row['u.user_id'];
       $date=$row["review_date"];
       $whenReview= strftime("On %b %d, %Y",strtotime($date));
       $review_body=$row["review"];
       $rating=$row["book_rating"];
  }
  }

    ?>
    <div><i>Breadcrumbs: </i><a href="admincontrol.php">Admin Home</a></div><br/>
    <?php if ($errormsg=="There is no new book reviews that need moderation.")
             {echo $errormsg;
             echo '<br/><br/>';}
          else
          {?>
    <table>
    <tr><td><?php echo $whenReview;?>&nbsp; <?php echo $review_author;?> gave a rating of <?php echo $rating;?> on <?php echo $bname;?> and said:</td></tr>
    <tr><td><?php echo $review_body;?></td></tr>
    <tr><td><form action="" method="POST" name="mod_form">
    Choose your action: <select name="mod_action" id="mod_action">
    <option value="n" selected>Please select</option>
    <option value="a">Accept</option>
    <option value="r">Reject</option>
    </select>
    <input name="book" id="book" value="<?php echo $bid;?>"/>
    <input type="hidden" name="user" id="user" value="<?php echo $uid;?>"/>
    <input type="submit" name="mod_save" id="mod_save" value="Save Moderation"/>
    </form>
    </td></tr>
    </table>
    <?php }?>
    </div>

        <div class="clearer">&nbsp;</div>


    <?php include "footer.php";?>

</div>
</div>
</div>
</div>
</div>
</body>
</html>

 [/code]

1 个答案:

答案 0 :(得分:3)

查询的结构没有错。这是错的:

$bid=$row['b.book_id'];

这是错误的:

$uid=$row['u.user_id'];

使用

$bid=$row['book_id'];

$uid=$row['user_id'];

代替。

另一方面,查询不是很易读。使用显式连接而不是隐式连接:

SELECT b.book_id,u.user_id,user_name,book_name,review_date,book_rating,review
FROM tbl_books b
INNER JOIN tbl_book_reviews br ON b.book_id=br.book_id 
INNER JOIN tbl_user u ON u.user_id=br.user_id
WHERE  moderated='n'
ORDER BY review_date ASC
LIMIT 1

这样做的好处是,连接条件接近于连接的表,而不是WHERE子句。