选择查询返回一个匹配记录

时间:2013-10-08 10:18:31

标签: php mysql

大家好,我的第一个问题在这里请指导我,我在一个变量中接收id's它可能是2,3,4代表这些id我从数据库中获取数据当我直接在数据库中运行查询时它工作正常当我在文件中使用它只返回第一个匹配记录所有其他左

$resid在这个varaibale中,我收到了变量

$query_responsibilities_RS = "SELECT * FROM responsibility  WHERE resID IN ($resIDs)";

这是我正在尝试的查询

$respons_RS = mysql_query($query_respons_RS, $timespace) or die(mysql_error());                                                     
$row_respons_RS = mysql_fetch_assoc($respons_RS);
$total_respons_RS = mysql_num_rows($respons_RS);

$rescID = $row_respons_RS["rescID"]; here i need records but it returns one record i want to use this $rescID in second query 

$query_rescategories_RS = "SELECT *  FROM responsibility_category WHERE rescID IN ($rescID)";
$rescategories_RS = mysql_query($query_rescategories_RS, $timespace) or die(mysql_error());
$row_rescategories_RS = mysql_fetch_assoc($rescategories_RS);
$total_rescategories_RS = mysql_num_rows($rescategories_RS);

<?php do{ 
          echo ("<h4>".addcslashes($row_rescategories_RS["rescName"],"\"")."</h4>");
        } while ($row_rescategories_RS = mysql_fetch_assoc($rescategories_RS)); ?>

我使用循环,但我只获得一条记录,但我有多条记录

3 个答案:

答案 0 :(得分:1)

你应该循环它 尝试以下代码

while($row_respons_RS = mysql_fetch_assoc($respons_RS)) {
 echo $row_respons_RS['rescID'];  //here you will get records 
}

答案 1 :(得分:0)

使用Ty:

do{
$rescID[] = $row_respons_RS["rescID"];
}while ($row_respons_RS = mysql_fetch_assoc($respons_RS));

然后:

$query_rescategories_RS = "SELECT *  FROM responsibility_category WHERE rescID IN (" . implode(",",$rescID) . ")";

答案 2 :(得分:0)

删除此行 $row_rescategories_RS = mysql_fetch_assoc($rescategories_RS);

$query_rescategories_RS = "SELECT *  FROM responsibility_category WHERE rescID IN ($rescID)";
$rescategories_RS = mysql_query($query_rescategories_RS, $timespace) or die(mysql_error());
$row_rescategories_RS = mysql_fetch_assoc($rescategories_RS);
$total_rescategories_RS = mysql_num_rows($rescategories_RS);

它只应在while循环中使用。

这里

<?php do{ 
          echo ("<h4>".addcslashes($row_rescategories_RS["rescName"],"\"")."</h4>");
        } while ($row_rescategories_RS = mysql_fetch_assoc($rescategories_RS)); ?>