1

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.

5
  • 1
    stackoverflow.com/questions/854128 will help you on to find duplicates in your database. Commented Oct 10, 2012 at 11:57
  • Let's know exactly at which point you are stuck. Commented Oct 10, 2012 at 11:59
  • I am getting data from a form via post, when i echo $sql the statement looks fine SELECT * FROM user WHERE username = 'svensenn' but i dont get a result Commented Oct 10, 2012 at 12:02
  • I see no error checking in your code so I wonder what the concrete problem is you run into. A database normally just works, if you're not confident with the outcome you should pinpoint where the problem to query the database first arises. This might need basic debugging first. Please do a error_reporting(~0); ini_set('display_errors', 1); at the very beginning of your script. Additionally you should enable error logging and follow the error log. Commented Oct 10, 2012 at 12:08
  • thank you hakre. i have tried $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); with a try/catch, but would't get an error. thank you for your tips, i didn't know how to do that. Commented Oct 10, 2012 at 12:15

2 Answers 2

2

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 use SELECT * if all you want is the number of rows that were found, and don't FETCH the full result-set to count them!

function uni($field,$value)
{
    $db = new PDO();//make connection, which you don't seem to do
    //or (not so good approach):
    //global $db;
    //Best approach would be to pass the connection to the function, as an extra argument, though
    $stmt = $db->prepare('SELECT '.$field.' FROM user WHERE '.$field.' = :value');
    if ($stmt->execute(array(':value' => $value)))
    {
        return $stmt->rowCount();
    }
    //query failed, throw errors or something
}

Read the docs for more examples.
Anyway, your code, in full should look like this:

function uni($field,$value,$db)
{
    $stmt = $db->prepare('SELECT '.$field.' FROM user WHERE '.$field.' = :value');
    if ($stmt->execute(array(':value' => $value)))
    {
        return $stmt->rowCount();
    }
    return false;
}
$username = $_POST['username']; 
$result = uni('username', $username,$pdo);//<--pass connection
Sign up to request clarification or add additional context in comments.

1 Comment

@SvenFischer: Glad I could help. I must say, this is only addressing the most "urgent" issue (getting the code to work). I left so many things out (like why SELECT * should be avoided, why you shouldn't fetch the results,... why Bobby Tables would find your code very interesting,...). Just google these three things, and find out more; it's well worth your while, I promise
1

You are using a reference to the $pdo object inside your function, but the $pdo object is not defined.

function uni($field, $value) {
  $sql= "SELECT * FROM user WHERE ".$field." = '".$value."'";
  $result = $pdo->query($sql);
//          ^^ undefined object

  $rows = $result->fetchAll(PDO::FETCH_COLUMN, 1);
  return count($rows);
}

Either pass the $pdo to your function, or make it a global.

Turn on error reporting, so you can see where the errors in your code are.

3 Comments

I have include ("dbconnect.php");
should i use it inside the function?
See my answer, pass it to the function like function uni($pdo, $field, $value) or make it a global $pdo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.