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

  • Home
  • SEARCH
  • 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 9190967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:34:33+00:00 2026-06-17T20:34:33+00:00

Here’s the problematic PHP function: //Get data associated with $criteria from db function getUserData($criteria,

  • 0

Here’s the problematic PHP function:

//Get data associated with $criteria from db
function getUserData($criteria, $value) {
    //obtain user data from db based on $criteria=$value
    global $pdo;
    //echo $criteria . " " . $value;
    try {
        $sql = 'SELECT id, first, last, email, userid FROM users WHERE :criteria= :value';
        //var_dump($sql);
        $st = $pdo->prepare($sql);
        $st->bindValue(':criteria', $criteria);
        $st->bindValue(':value', $value);
        $st->execute();
    }
    catch (PDOException $ex) {
        $error = "Failed to obtain user data.";
        $errorDetails = $ex->getMessage();
        include 'error.html.php';
        exit();
    }
    $row = $st->fetch();
    //var_dump($row);
        if ($row)
        {
            $userdata = array();
            $userdata['id'] = $row['id'];
            $userdata['first'] = $row['first'];
            $userdata['last'] = $row['last'];
            $userdata['email'] = $row['email'];
            $userdata['userid'] = $row['userid'];
            return $userdata;
        }
        return FALSE;
}

I use this function to return a whole row of data associated with specific column in it.

When used at it’s current state, with a call like that getUserData("email", "John_Stewart_2013"), it returns false, meaning an empty result, while the same query runs fine in MySQL CLI.

If I, on the other hand, substitute the query string $sql with :

$sql = "SELECT id, first, last, email, userid FROM users WHERE $criteria='$value'";

And comment out the bindValue calls, Every thing runs fine in PHP, and the query returns as desired.

But the problem is, those function arguments are user-submitted form data, meaning the solution is vulnerable to SQL Injection.

What’s wrong here in the first query form?

  • 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-17T20:34:34+00:00Added an answer on June 17, 2026 at 8:34 pm

    You can’t use bindValue with column names I’m afraid.

    If you think about what a prepared statement is, this should become more obvious. Basically, when you prepare a statement with the database server, it creates an execution plan for the query beforehand, rather than generating it at the time of running the query. This makes it not only faster but more secure, as it knows where it’s going, and the datatypes that it will be using and which are going to be input.

    If the column/table names were bindable in any way, it would not be able to generate this execution plan, making the whole prepared statement idea somewhat redundant.

    The best way would be to use a hybrid query like so:

    $sql = "SELECT id, first, last, email, userid FROM users WHERE $criteria = :value";
    

    I’m going to hope that the $criteria column isn’t entirely free form from the client anyway. If it is, you’d be best limiting it to a specific set of allowed options. A simplistic way to do would be to build an array of allowed columns, and check if it’s valid with in_array, like so:

    $allowed_columns = array('email', 'telephone', 'somethingelse');
    if (!in_array($criteria, $allowed_columns))
    {
        $error = "The column name passed was not allowed.";
        $errorDetails = $ex->getMessage();
        include 'error.html.php';
        exit;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my function: function reCalculate(i) { document.getElementById(Q + i).value = document.getElementById(C + i).value
Here is my code <?php function random_id() { $chars = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $id = '';
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is what I am currently doing. PHP echo's out the recent post in
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here are my functions: function dropmenu() { $(.dropdownitem).show('slow'); } function hidemenu() { $(.dropdownitem).hide('slow'); }
Here is the two scripts I have Script 1: <? include('config.php'); $json = $_POST['payload'];
Here is a scenario: User installs .NET application that you have made. After some

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.