douyongwan5946 2012-04-13 17:22
浏览 22
已采纳

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

can you explain me why when i try this query it returns to me only half of rows?

For example, if $records is made by 4 values it only get row 1 and 3 from DB.

What's wrong?

                $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条回答 默认 最新

  • douqujin2767 2012-04-13 17:24
    关注

    Are you overwriting your $fetch variable in the loop? Maybe try:

    $fetch2=mysql_fetch_assoc($query);
    

    Or, even better, use a join in your 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."')");
    

    Then just get everything you want out of that single query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角