如何在php中比较两个mysql表

时间:2011-04-01 05:53:53

标签: 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

1 个答案:

答案 0 :(得分:1)

尝试编写以下内容

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

这样,数组中的值将是字符串,可以轻松比较。我不知道array_intersect如何处理嵌套数组,但它可以很好地处理字符串。