MySQL查询表-不显示第一个结果 [英] MySQL querying table - Doesn't show first result
问题描述
我有一个具有以下结构的数据库(四边形):
I have a DB (quartos) created with the following structure:
id_quarto tipo_quarto vista_quarto |
a Single Mar |
b Single Mar |
c Single Mar |
d Single Serra |
当tipo_quarto = Single和vista_quarto = Mar哪个值来自表单时,我希望它返回id_quarto的结果.
I want it to return the results of id_quarto, when tipo_quarto=Single and vista_quarto=Mar which values come from a form.
所以我写了以下内容:
$strSQL = "
SELECT id_quarto
FROM quartos
WHERE tipo_quarto='". $_POST['tipo_quarto'] ."'
AND vista_quarto='". $_POST['vista_quarto'] ."'";
$rs = mysql_query($strSQL);
$row = mysql_fetch_array($rs);
然后我将其循环并按如下所示写入表中:
Then i loop it and write in a table as followed:
while($row = mysql_fetch_array($rs)) {
<?
<table border="1">
<tr align="left">
<td width="75"><?php echo $row['id_quarto']; ?></td>
</tr>
</table>
这里的问题是它不返回id_quarto = a,仅返回b和c.为什么会这样,我该怎么解决?
The problem here is that it does not return the id_quarto=a , only b and c. Why is that and what can i do to fix it?
谢谢.
推荐答案
在mysql_query($strSQL);
之后,您还有多余的$row = mysql_fetch_array($rs);
.然后在while
循环中,再次读取$row
(结果集中的第二行).
这样您的代码就会看起来
You have extra $row = mysql_fetch_array($rs);
just after mysql_query($strSQL);
. Then in while
loop you read $row
again (second row in resultset).
So your code will look
$strSQL = "SELECT id_quarto FROM quartos
WHERE tipo_quarto='". $_POST['tipo_quarto'] ."'
AND vista_quarto='". $_POST['vista_quarto'] ."'";
$rs = mysql_query($strSQL);
// $row = mysql_fetch_array($rs); Don't need this line!!!
while($row = mysql_fetch_array($rs))
{
// output ....
}
此外,添加用于处理mysql错误的代码总是很有意义.
Also, it always makes sense to add code for handling mysql errors.
这篇关于MySQL查询表-不显示第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!