Jump to content

Check table for duplicate entries


tcorbeil

Recommended Posts

I'm using the following code to look into a table for a duplicate value..

 

$query="SELECT * FROM UserName WHERE UserName = '$UserName'";

$result=mysql_query($query);

$row = mysql_fetch_array($result); // got the data now

$filename = $row['refdatabase'];

 

only problem is if there are no matches, I get an error.. i just want to check for duplicate entries and come back with a variable = 1 (yes we have a duplicate) or variable = 0 (no, we don't have a match.)

 

any help would be appreciated..

 

T.

Link to comment
https://forums.phpfreaks.com/topic/45653-check-table-for-duplicate-entries/
Share on other sites

Try this out.  You can do slightly better using "exists", but I don't think it's worth the effort.

 

$query="SELECT * FROM UserName WHERE UserName = '$UserName'";
$result=mysql_query($query) or die("Query failed: $query\nError: " . mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows == 1) {
  # dup
} else {
  # not dup
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.