Bilal Ahmad
Bilal Ahmad

Reputation: 1

Select query return one matching record

hello everyone its my first question here kindly guide me i am receiving id's in a variable it may be 2,3,4 on behalf of these id's i am fetching data from database when i run query directly in the databse it work fine when i use in file it returns just first matching record all other left

$resid in this varaibale i receive varaibles

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

this is the query i am trying

$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)); ?>

i use loop but i am getting only one record but i have more than one record

Upvotes: 0

Views: 104

Answers (3)

Dinesh
Dinesh

Reputation: 4110

remove this line $row_rescategories_RS = mysql_fetch_assoc($rescategories_RS);

from

$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);

it should only be use in while loop.

here

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

Upvotes: 0

Ivan Nikolaievskyi
Ivan Nikolaievskyi

Reputation: 105

Ty to use :

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

and then:

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

Upvotes: 0

user2092317
user2092317

Reputation: 3338

you should loop it try the following code

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

Upvotes: 1

Related Questions