usii
usii

Reputation: 1113

How to find data when results set is in array?

i want to find data through id, i have an array like that

$result= 

 Array
 (

   [3] => 536371014
   [38] => 1435902884
   [53] => 100000224980743
)

user _id is

  [3], [38], [53]

in my user tabe

    user_id    name 
     3          usii
     38         test
     53         test 2

i want to find all data through user id, how can i do that with the result set user id which is index, i tried alot but didn't get success, please help me to do that, thanks a ton in advance.

Upvotes: 6

Views: 114

Answers (3)

rynhe
rynhe

Reputation: 2529

$user_ids = implode(",",array_keys($result));
$query = "SELECT * FROM usertable WHERE user_id IN (".$user_ids.")";

Upvotes: 2

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40051

Something like this should work (brutal pseudo code)

in_list = implode(", ", array_keys($result));
$all_users = mysql.query("select * from user_table where user_id in (" . in_list . ");";

array_keys should give you an array of just the indexes used (3, 38 and 53)
implode with ", " as glue should give you a string like this "3, 38, 53" suitable for a mysql IN condition.

Upvotes: 3

chandresh_cool
chandresh_cool

Reputation: 11830

Use foreach like this

foreach($result as $val) {

   echo $val[$user_id];
}

Upvotes: 6

Related Questions