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 8533259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:55:19+00:00 2026-06-11T09:55:19+00:00

I wrote a function that selects all the users from my database and all

  • 0

I wrote a function that selects all the users from my database and all cooresponding fields using PDO. I also allowed the function to take an array as a parameter to allow for easy filtering. My problem is when I pass the filter array to the execute function I don’t get any rows from my tables. I know it has something to do with PDO and the way I’m passing my array into it because when I run the execute method with the filter hard coded into the query it works. Here is my function and the code that is calling it:

$config = array(
    'filters' => array(
        'all members'     => array('',  ''),
        'officers'            => array('statuses.position_id', '8'),
        'current members'     => array('users.alumni', 0)
    )
);

$filter = $config['filters'];
//Requested filter is just a constant used to keep track of the parsed $_GET value
$result = $dbh->getUsers($filter[REQUESTED_FILTER]);

//Just showed function, not whole class
public function getUsers(array $filter = array('', '')) {
  $result = array();

  $sql = 'SELECT users.firstname, users.lastname, users.grad_year, users.alumni, users.signedISA, phone_numbers.phone_number, emails.email_address,
                addresses.name, addresses.street, addresses.city, addresses.state, addresses.zip
           FROM 
              ((users LEFT JOIN phone_numbers 
                 ON users.user_id = phone_numbers.user_id) 
                 LEFT JOIN emails 
                 ON users.user_id = emails.user_id)
                 LEFT JOIN addresses
                 ON users.user_id = addresses.user_id
                 WHERE ? = ?;';

  $sth = $this->handle->prepare($sql);

  if($sth->execute($filter)) {
     while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        array_push($result, $row);
     }
  }

  return $result;
}

Anyone have any idea why this isn’t working?

  • 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-11T09:55:20+00:00Added an answer on June 11, 2026 at 9:55 am

    You can’t pass schema object identifiers (like table or column names) as parameters to a prepared statement (your current attempt is always comparing parameterised string literals from within the WHERE clause).

    Instead, you will need to do something like:

    public function getUsers(array $filter = array('\'\'', '')) {
      $result = array();
    
      $sql = 'SELECT users.firstname, users.lastname, users.grad_year, users.alumni, users.signedISA, phone_numbers.phone_number, emails.email_address,
                    addresses.name, addresses.street, addresses.city, addresses.state, addresses.zip
               FROM 
                  ((users LEFT JOIN phone_numbers 
                     ON users.user_id = phone_numbers.user_id) 
                     LEFT JOIN emails 
                     ON users.user_id = emails.user_id)
                     LEFT JOIN addresses
                     ON users.user_id = addresses.user_id
                     WHERE ' . $filter[0] . ' = ?;';
    
      $sth = $this->handle->prepare($sql);
    
      if($sth->execute($filter[1])) {
         while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            array_push($result, $row);
         }
      }
    
      return $result;
    }
    

    Beware of SQL injection if the value of $filter is outside of your control (you’ll need to quote identifiers with backticks and double-up any backticks contained therein: make sure you do this in a multi-byte safe way!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I'm trying to write a function that would check in my users table
I wrote a function that scans a tab delimited file of baseball stats. public
So I wrote this function that is given possible numbers, and it has to
i wrote a custom jquery function that accept a callback but not being able
I wrote a simple function, that makes emacs add matching quotes (so when I
I've got a function I wrote quite some time ago that works fine, but
I have a C++ DLL that I wrote that has a single exposed function,
I want to write a function that identifies all the links on a particular
I need to write a function that can take an if statement at runtime
I'm using a generic system for reporting which takes data from a database view

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.