Possible Duplicate:
Find duplicate records in MySQL
I am a newbe trying to find duplicates in a database with this function:
function uni($field, $value) {
$sql= "SELECT * FROM user WHERE ".$field." = '".$value."'";
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_COLUMN, 1);
return count($rows);
}
$username = $_POST['username'];
$result = uni("username", $username);
…i am about to bang my head against something solid. For some reason the query won’t return a result and I dont know why.
Ok, so you’re using
PDO, good. Your snippet is still wide open to injection attacks, though: you’re still passing raw user input to a query. Also, don’t useSELECT *if all you want is the number of rows that were found, and don’t FETCH the full result-set to count them!Read the docs for more examples.
Anyway, your code, in full should look like this: