PHP script for checking duplicates in two MySQL tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

PHP script for checking duplicates in two MySQL tables

Post by JKM »

Hi there,

I'm having two tables in my database, and I wan't to check if the fruits (ex: banana) is matches any rows in the table "check" in the row "list". If there is any matches, $checkdupe == 'yes ('.$fetch1['amount'].')'; if not, $checkdupe == 'no';.

Code: Select all

<table>
    <tr>
        <td>FRUITS</td>
        <td>check</td>
    </tr>
<?php
mysql_connect('xxx','xxx','xxx') or die("mysql error");
mysql_select_db("xxx") or die("mysql error");
 
$sql = mysql_query("SELECT DISTINCT FROM fruits");
$sql2 = mysql_query("SELECT * FROM check WHERE list='".$fetch['fruit']."'");
$query = mysql_query($sql) or die(mysql_error());
 
while($fetch = mysql_fetch_array($query)) {
?>
    <tr>
        <td><?php echo $fetch['fruit']; ?></td>
        <td><?php $checkdupe; ?></td>
    </tr>
<?
}
?>
</table>
 
Thanks for any help!
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP script for checking duplicates in two MySQL tables

Post by susrisha »

have you forgotten something??
You need to tell us what seems to be the problem with the code..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP script for checking duplicates in two MySQL tables

Post by josh »

Select all values, keep an array keyed by primary key, with a value of an integer representing how many times that particular value has been counted, then prune out anything with a value of 1 and run it thru array_keys
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP script for checking duplicates in two MySQL tables

Post by VladSun »

I doubt you will need any PHP code for doing this, but still I can't understand your DB design.
Could you post your DB schema and some real world values from you tables?
There are 10 types of people in this world, those who understand binary and those who don't
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP script for checking duplicates in two MySQL tables

Post by josh »

He's trying to normalize data that is not in 2nd NF ( repeating data in rows ), apparently a fruit tracking app lol
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP script for checking duplicates in two MySQL tables

Post by VladSun »

josh wrote:apparently a fruit tracking app lol
:twisted:
There are 10 types of people in this world, those who understand binary and those who don't
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP script for checking duplicates in two MySQL tables

Post by josh »

I'll sue JKM after I stage an accident choking to death on an RFID chip from his banana
Post Reply