tcorbeil Posted April 5, 2007 Share Posted April 5, 2007 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 More sharing options...
btherl Posted April 5, 2007 Share Posted April 5, 2007 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 } Link to comment https://forums.phpfreaks.com/topic/45653-check-table-for-duplicate-entries/#findComment-221732 Share on other sites More sharing options...
Vikas Jayna Posted April 5, 2007 Share Posted April 5, 2007 alternatively! the following can also be tried: $query="SELECT count(*) FROM UserName WHERE UserName = '$UserName'"; $result=mysql_query($query); $row = mysql_fetch_row($result); if($row[0] > 1) // dup else // not dup Link to comment https://forums.phpfreaks.com/topic/45653-check-table-for-duplicate-entries/#findComment-222206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.