Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:57:22+00:00 2026-06-12T14:57:22+00:00

Possible Duplicate: Find duplicate records in MySQL I am a newbe trying to find

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:57:23+00:00Added an answer on June 12, 2026 at 2:57 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to implement re-ordering of CoreData records? I'm trying to find a
I want to find possible candidate duplicate records in a large database matching on
Possible Duplicate: How to delete duplicate records in mysql database? there are duplicate rows
Possible Duplicate: error: Unable to find vcvarsall.bat I'm trying to install MySql interface for
Possible Duplicate: SQL - find records from one table which don't exist in another
Possible Duplicate: How to find out where a function is defined? get filename of
Possible Duplicate: How to find out where a function is defined? I am working
Possible Duplicate: Find common values in multiple arrays with PHP I'm trying to find
Possible Duplicate: Find url from string with php I found this code: but 2
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.