select query和while循环不能正确返回所有行

时间:2012-04-13 17:22:45

标签: php mysql select while-loop

你能解释一下为什么当我尝试这个查询时它只返回给我一半的行吗?

例如,如果$ record由4个值组成,则只从DB获得第1行和第3行。

怎么了?

                $query=mysql_query("SELECT * FROM ".DB_PREF."books WHERE book_id IN ('".$records."')");
                while($fetch=mysql_fetch_assoc($query))
                {
                    global $book_id, $book_title, $book_description, $book_author_id, $book_author_name, $book_author_surname;
                    $book_id=$fetch['book_id'];
                    $book_title=$fetch['book_title'];
                    $book_description=$fetch['book_description'];
                    $book_author_id=$fetch['book_author_id'];
                    $query=mysql_query("SELECT * FROM ".DB_PREF."profiles WHERE user_id='".$book_author_id."'");
                    $fetch=mysql_fetch_assoc($query);
                    $book_author_name=$fetch['user_name'];
                    $book_author_surname=$fetch['user_surname'];
                    getModule('htmlmodule...');
                }

2 个答案:

答案 0 :(得分:2)

你是否在循环中覆盖你的$ fetch变量?也许试试:

$fetch2=mysql_fetch_assoc($query);

或者,更好的是,在SQL中使用连接:

$query=mysql_query("SELECT * FROM ".DB_PREF."books LEFT JOIN ".DB_PREF."profiles ON book_author_id = user_id WHERE book_id IN ('".$records."')");

然后从单个查询中获取您想要的所有内容。

答案 1 :(得分:0)

我的意思

mysql_select_db($database_lolx, $lolx);
$query_Recordset1 = "SELECT * FROM person";
$Recordset1 = mysql_query($query_Recordset1, $lolx) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
<table border="1">
  <tr>
    <td>id</td>
    <td>emal</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Recordset1['id']; ?></td>
      <td><?php echo $row_Recordset1['emal']; ?></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>