473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to compare two mysql tables in php

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

Expand|Select|Wrap|Line Numbers
  1. $array1=array();
  2. $array2=array();
  3. $query2="select name from movie where user_id='1'";
  4. $result2=mysql_query($query2) or die(mysql_error());
  5. while($rss = mysql_fetch_assoc ($result2))
  6. {
  7.     $array1[]=$rss;
  8. }
  9. print_r($array1);
  10.  
This will print
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Array ( [name] => Snatch ) [1] => Array ( [name] => The Social Network Movie )<br />[2] => Array ( [name] => Death Note ) [3] => Array ( [name] => Titanic ) <br />[4] => Array ( [name] => Once Upon a Time in the West ) ) 
And for second user
Expand|Select|Wrap|Line Numbers
  1. $query3="select name from movie where user_id=1";
  2. $result3=  mysql_query($query3) or die(mysql_error());
  3. while($rss1=  mysql_fetch_assoc($result3))
  4. {
  5.     $array2[]=$rss1;
  6. }
  7. print_r($array2);
This will print
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Array ( [name] => The Lord of the Rings Trilogy ) [1] => Array ( [name] => Snatch ) <br />[2] => Array ( [name] => The Social Network Movie ) [3] => Array ( [name] => Scarface ) <br />[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 ) <br />[7] => Array ( [name] => Butch Cassidy and the Sundance Kid ) [8] => Array ( [name] => Fracture ) <br />[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.
Expand|Select|Wrap|Line Numbers
  1. $match= array_intersect($array1, $array2);
  2. print_r($match);
The result will is
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Array ( [name] => Snatch ) [1] => Array ( [name] => The Social Network Movie )<br />[2] => Array ( [name] => Death Note ) [3] => Array ( [name] => Titanic ) <br />[4] => Array ( [name] => Once Upon a Time in the West ) ) 
But the Common movies are:
Expand|Select|Wrap|Line Numbers
  1.  Snatch , The social network movie , once upon a time in the west
Apr 1 '11 #1
2 3175
code green
1,726 Expert 1GB
array_intersect() can only return values present in $array1.
Probably easiest thing to do is merge the two and compare.
Expand|Select|Wrap|Line Numbers
  1. array_intersect(array_merge($array1,$array2),$array1,$array2); 
Apr 1 '11 #2
dgreenhouse
250 Expert 100+
Other than the fact that your tables should be normalized, you can just do a self-join on the table and get the results in one query.

Haven't tested, but I believe this should work:

select a.name from movie a
join movie b on b.name = a.name and b.user_id = 2
where a.user_id = 1

The above is one of a few was of creating the query and any common movies between users should be in the result set.
Apr 1 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Perttu Pulkkinen | last post by:
I want to copy certain tables from server to server b. How do I make it fastly and efficiently with PHP? I wrote below my current start, but it may not be the best approach. Perttu Pulkkinen,...
0
by: jerome iffrig | last post by:
Hi, I am willing to setup my MySQL tables so they can hold international strings (English, French, German and Spanish). I understand that Unicode character coding is a good candidate to do so....
0
by: buster100 | last post by:
I am looking for an example of php code that accesses a mysql table using SSL (php v4.3.10). I have php code on serverA and some mysql tables on serverB (located on the other side of the internet...
2
by: Ian N | last post by:
I have a website which puts customers into different groups, depending on the referrer they can see certain products. So for example if the referrer code = 1, they'll only be able to see the...
10
by: xixi | last post by:
we are using db2 udb v8.1 on windows, i would like to know whether we have tool or way to compare two tables on same database for data difference. thanks. (same ddl, different data, try to compare...
0
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
0
by: data monkey | last post by:
My company's web application uses mySQL databases. I have linked these tables (Read only) into Access 2007 so I can create queries and reports. Now I need to extract some of the content from these...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
2
by: neerja khattar | last post by:
I have total 3 databases.One is mysql and other 2 are of access type. I have 10 tables in each of the databases. I want to sync 2 tables from both access databases with mysql tables. whatever...
1
by: Sajad Bhat | last post by:
I have MySQL tables in Ms. Notepad, how can i put them in MySQL Server and want to extract all the required information from these tables to make an excelsheet.please give the answer in a detailed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.