check for duplicate record

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
crimsonluv
Forum Newbie
Posts: 9
Joined: Tue Mar 23, 2004 9:10 pm
Contact:

check for duplicate record

Post by crimsonluv »

is it possible for me to check duplicate record using mysql_fetch_object instead of mysql_fetch_array?

$dbquery = "SELECT * FROM Staffs_Category";
$result = mysql_query($dbquery, $dblink);
while($row = mysql_fetch_object($result)){
$limit = count($row);

}
for($i = 0; $i<limit; $i++) {
if($row == $staffcat || $row == $other) {
echo 'category entered is already in record. please re enter';
}
}


any mistake here?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not really a good idea to select all the rows just to see if one exists. Might be better to do a query like ,
SELECT COUNT(*) FROM Staffs_Category WHERE cat='$staffcat' .. then use mysql_num_rows() to see if any rows were returned, if there were then you know it's a duplicate.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I had a similar problem...

viewtopic.php?t=19442
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

<-- what i always do

Code: Select all

require ("dbhandler.php");
$dbquery = "SELECT something FROM somewhere WHERE username = $username"; 
$result = mysql_query($dbquery) or die (mysql_error()); 
$resultarray = mysql_fetch_array($result, MYSQL_ASSOC);
$affected = mysql_num_rows($result);
// query complete
// now lets check for a previous match
if ($affected >= 1)
{
// there is already a match in the database
// do something
exit();
}
else
{
// continue, the username wasnt already in the database
}
dunno whether this is good or bad but its the way i always do it and it works

mal
crimsonluv
Forum Newbie
Posts: 9
Joined: Tue Mar 23, 2004 9:10 pm
Contact:

Post by crimsonluv »

thanks for your reply

i'll try again and see what happen :)

thanks a lot!
Post Reply