0

I've got this mysqlquery:

$Nusers = mysql_query("SELECT user_id FROM dotp_user_task_type WHERE user_task_types_id = '$select1'");
$row = mysql_fetch_array($result);

It takes all users id's which has task with specific id. So, now i need to take their name and last name but i don't know how to use WHERE when $row is array with numbers.

$Nusers = mysql_query("SELECT CONCAT(contact_first_name, ' ', contact_last_name) FROM dotp_user_contacts WHERE user_id = '$row'");

Help please.............

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
McLaren
  • 776
  • 1
  • 10
  • 23

3 Answers3

2

Why don't you combine both the queries like

SELECT CONCAT(contact_first_name, ' ', contact_last_name) as fullname
FROM dotp_user_contacts
WHERE user_id  IN ( SELECT distinct user_id FROM dotp_user_task_type 
WHERE user_task_types_id = '$select1' );

(OR) Using a simple JOIN like

SELECT CONCAT(duc.contact_first_name, ' ', duc.contact_last_name) as fullname
FROM dotp_user_contacts duc 
JOIN  dotp_user_task_type dtt ON duc.user_id = dtt.user_id   
WHERE dtt.user_task_types_id = '$select1';
Rahul
  • 76,197
  • 13
  • 71
  • 125
2

Use IN -

" ....WHERE user_id IN (" .implode(",", $row). ")"
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
2

Try this..

Using IN to search array value in mysql

$datauserid=array();

    $Nusers = mysql_query("SELECT user_id FROM dotp_user_task_type WHERE user_task_types_id = '$select1'");

    while($row = mysql_fetch_array($Nusers))
{
$datauserid[]=$row['user_id'];
}
    $userid=implode(",",$datauserid);

    $Nusers = mysql_query("SELECT CONCAT(contact_first_name, ' ', contact_last_name) FROM dotp_user_contacts WHERE user_id IN ($userid)");
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
  • Your suggestion is best but how can i make it write only one time id because it writes `( [0] => 38 [user_id] => 38 )` and it looks like 38, 38 it will look two times? Its when i fetch array – McLaren Aug 11 '15 at 09:54
  • It worked perfectly, i need one more thing. is it possible to give user_id for that [user_id] => contatct_fist_name contact_last_name? Because now its just increasing numbers – McLaren Aug 11 '15 at 10:44
  • Here go to this http://stackoverflow.com/questions/31940099/make-array-keys-user-id – McLaren Aug 11 '15 at 11:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86689/discussion-between-deena-and-mclaren). – Deenadhayalan Manoharan Aug 11 '15 at 11:12
  • i have answered this question.check it and let me know – Deenadhayalan Manoharan Aug 11 '15 at 11:13