2011-04-01 4 views
0

나는 영화 테이블을 가지고 있고 두 명의 사용자의 공통 영화를 비교하고 싶습니다.PHP에서 두 개의 mysql 테이블을 비교하는 방법

 
$array1=array(); 
$array2=array(); 
$query2="select name from movie where user_id='2'"; 
$result2=mysql_query($query2) or die(mysql_error()); 
while($rss = mysql_fetch_assoc ($result2)) 
{ 
    $array1[]=$rss; 
} 
print_r($array1); 

Array ([0] => Array ([name] => Snatch) [1] => Array ([name] => The Social Network Movie)
[2] => Array ([name] => Death Note) [3] => Array ([name] => Titanic)
[4] => Array ([name] => Once Upon a Time in the West))

를 인쇄합니다 그리고 난 그것이 나에게 첫 번째 배열을 제공 이러한 두 배열을 비교하면 두 번째 사용자

$query3="select name from movie where user_id=1"; 
$result3= mysql_query($query3) or die(mysql_error()); 
while($rss1= mysql_fetch_assoc($result3)) 
{ 
    $array2[]=$rss1; 
} 
print_r($array2);
위해이

Array ([0] => Array ([name] => The Lord of the Rings Trilogy) [1] => Array ([name] => Snatch) 
[2] => Array ([name] => The Social Network Movie) [3] => Array ([name] => Scarface)
[4] => Array ([name] => Once Upon a Time in the West) [5] => Array ([name] => Legend of the Guardians: The Owls of Ga'Hoole) [6] => Array ([name] => Once Upon a Time in America)
[7] => Array ([name] => Butch Cassidy and the Sundance Kid) [8] => Array ([name] => Fracture)
[9] => Array ([name] => Invictus) [10] => Array ([name] => Pride and Glory) [11] => Array ([name] => Casablanca))

를 인쇄합니다.

$match= array_intersect($array1, $array2); 
print_r($match);
결과의 의지
Array ([0] => Array ([name] => Snatch) [1] => Array ([name] => The Social Network Movie)
[2] => Array ([name] => Death Note) [3] => Array ([name] => Titanic)
[4] => Array ([name] => Once Upon a Time in the West))

하지만 일반적인 영화는 다음과 같습니다Snatch , The social network movie , once upon a time in the west

+0

왜 mysql에서 그렇게하지 않습니까? – zerkms

+0

이 코드를 소스에서 직접 복사/붙여 넣기 했습니까? 여기 $ query2와 같은 user_id로부터 $ query3이 나오기 때문에 $ array1에있는 모든 것을 출력 할 것입니다. – tkm256

+0

@zerkms : DB 디자인이 좋지 않기 때문에 –

답변

1

이 배열의 값이 문자열이 될 수 있습니다 것입니다 그런 식으로 대신

while($rss = mysql_fetch_assoc ($result2)) { $array1[]=$rss['name']; }

다음 쓰기 시도 쉽게 비교할 수 있습니다. array_intersect 내포 된 배열을 처리하는 방법을 내 머리 꼭대기의 잘 모르겠지만 문자열로 잘 작동합니다.

관련 문제