2014-03-04 3 views
0

mysql 데이터베이스에서 데이터를 가져 오려고합니다 ... 그러나 배열에 의해 지정된 결과 만 필요합니다 ... 예를 들어 멤버 데이터로 채워진 데이터베이스가 있습니다 : 사용자 이름, 이메일 등 ... 그리고 회원마다 연락처 목록을 저장하는 다른 테이블과 같은 ... 그래서 회원 목록을 표시하는 방법을 찾고 있습니다 ... 나는 최선의 방법이 무엇인지 명확하지 않습니다. 어떤 조언도 환영합니다. 예를 들면 다음과 같습니다.mysql에서 배열 특정 데이터 가져 오기

$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[]""; 

설명하기가 조금 힘듭니다 ... 누군가 내 뜻을 알기를 바랍니다.

답변

2

IN 당신이

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

$c_array,으로 폭파 member id을 구성된 문자열임을 고려 좋아하는 데 도움이 될 것입니다. JOIN를 사용

0

사용 In

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

0

당신은 주어진 멤버의 연락처 목록을 얻을 수 있습니다 :

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

JOINS

0

이상한 프로그램 다음 하위 쿼리를 효율적입니다.

$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. **// 
관련 문제