如何在php中比较两个mysql表 [英] how to compare two mysql table in php

查看:39
本文介绍了如何在php中比较两个mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电影表,我想比较两个用户的共同电影.

<前>$array1=array();$array2=array();$query2="select name from movie where user_id='2'";$result2=mysql_query($query2) 或 die(mysql_error());while($rss = mysql_fetch_assoc ($result2)){$array1[]=$rss;}打印_r($array1);

这将打印

Array ( [0] => Array ( [name] => Snatch ) [1] => Array ( [name] => The Social Network Movie )
[2] => 阵列([名称] => 死亡笔记)[3] => 阵列([名称] => 泰坦尼克号)
[4] => 阵列([名称] => 西部往事))

对于第二个用户

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

这将打印

Array ( [0] => Array ( [name] => 魔戒三部曲 ) [1] => Array ( [name] => Snatch ) 
[2] => Array ( [name] => The Social Network Movie ) [3] => Array ( [name] => Scarface )
[4] => Array ( [name] => 西部往事 ) [5]=> 阵列([名称] => 守护者传说:Ga'Hoole 的猫头鹰)[6] => 阵列([名称] => 美国往事)
[7] => 阵列( [name] => Butch Cassidy and the Sundance Kid ) [8] => Array ( [name] => Fracture )
[9] => Array ( [name] => Invictus ) [10] =>阵列([名称] => 骄傲与荣耀)[11] => 阵列([名称] => 卡萨布兰卡))

当我比较这两个数组时,它给了我第一个数组.

$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] => 一次从前在西方 ) )

但是比较常见的电影有: Snatch ,社交网络电影,从前在西方

解决方案

尝试编写以下内容

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

这样数组中的值将是字符串并且可以很容易地进行比较.我不知道 array_intersect 如何处理嵌套数组,但它可以很好地处理字符串.

i have a movie table and i want to compare the common movies of two users.

$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);

This will print

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 ) )

And for second user

$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);

This will print

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 ) )

When i compare these two arrays it gives me the first array.

$match= array_intersect($array1, $array2);
print_r($match);

The result will is

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 ) )

But the Common movies are: Snatch , The social network movie , once upon a time in the west

解决方案

Try writing the following instead

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

That way the values in the arrays will be strings and can be compared easily. I don't know of the top of my head how array_intersect handles nested arrays but it works fine with strings.

这篇关于如何在php中比较两个mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆