[英]select query and while loop doesn't return all rows correctly
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. 例如,如果$ record由4个值组成,则只从DB获取第1行和第3行。
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...');
}
Are you overwriting your $fetch variable in the loop? 你是否在循环中覆盖你的$ fetch变量? Maybe try: 也许试试:
$fetch2=mysql_fetch_assoc($query);
Or, even better, use a join in your SQL: 或者,更好的是,在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. 然后从单个查询中获取您想要的所有内容。
MY DO WHILE 我做什么
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.