1

I'm trying to get data from a mysql database... but I only need results specified by an array... for example I have a database which is populated with member data: username, email etc... and another table where I store per member their contact list like... So I'm looking on a way to display member their contact list... I'm just not clear on whats the best way... any tips are welcome... for example:

$result = mysql_query("select contact_id from member_contact_list");

$contacts = array();

while ($row = mysql_fetch_array($result)){  
   $c_array[] = $row[ 'username' ];
}

$c_array = implode( ',', $existing_users );

$sql = "SELECT * FROM members WHERE id="c_array[]"";

Its a little hard to explain... I hope someone gets what I mean.

4 Answers 4

2

IN will help you like

$sql = "SELECT * FROM members WHERE id IN (".$c_array.")";

Consider that $c_array is an string that consists member id imploded with ,.

Sign up to request clarification or add additional context in comments.

Comments

0

Use In

$sql = "SELECT * FROM members WHERE id in (".$c_array.")";

Comments

0

Using JOIN you can get the contact list of a given member:

SELECT * FROM members AS m
LEFT JOIN
contacts AS c ON (m.user_id = c.user_id)
WHERE m.user_id = 1;

JOINS are efficient then sub-queries

Comments

0

Strange program.

$result = mysql_query("select contact_id from member_contact_list");

$contacts = array(); //**not used anywhere else in the given code**

while ($row = mysql_fetch_array($result)){  
   $c_array[] = $row[ 'username' ]; //**was that supposed to be $contacts?**
   //** also was this the right version: $contacts = $row['id']; ??? **//
}

$c_array = implode( ',', $existing_users ); 
//** where was $existing_users defined? Was that supposed to be $contacts, too?**

$sql = "SELECT * FROM members WHERE id="c_array[]"";
//** correct version is indeed with the clause "WHERE id IN (".$c_array.")" **;

//** last but not least, the $c_array seems to be the collection of user names, not user ids. **//

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.