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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:53:22+00:00 2026-06-10T10:53:22+00:00

I have been old school using mysql_query and starting out now using PDO. Which

  • 0

I have been old school using mysql_query and starting out now using PDO. Which is great!

But in my old scripts I had build a dynamic query builder, and i’m having a tough time porting that over using PDO.

If anyone can give me some direction that would be great!

Here is the theory of it.

  1. I have an array of
  2. the DB Fields and Values (upon insert).
  3. Create the query string to product a valid PDO transaction

Here is a portion of what i’m trying to do.

public $dbFields; // This is an array of the fields plus VALUES

public function select($where, $limit) {
    // This is what I **had** before
    $query = "SELECT ". implode(", ", $this->dbFields) ." FROM ". $this->table." WHERE ". $where ." ". $limit."";

    // Now i need to convert that to PDO
    $this->connection->beginTransaction();

    # START Query
    $select = $this->connection->prepare("SELECT {$this->fieldNames} FROM {$this->table}");

    // I need to BIND my params and values, but i'm not sure the best route to take when I have a WHERE clause that includes, "AND" / "OR" operators.

    # EXECUTE the query
    $select->execute();

    $this->connection->commit();
}

This is what I HAD before

$results = $db->select("userId = 111 OR userId = 222");

But what i’m thinking I need to do is use something more like

$results = $db->select(array("userId"=>111, "userId"=>222));

I know this is a tall order, and I hope it makes sense in what i’m trying to do, but any help in trying to build these queries would be greatly appreciated.

  • 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-10T10:53:24+00:00Added an answer on June 10, 2026 at 10:53 am

    You’ll need a separate $params parameter to your select method. I took the liberty of providing defaults for the method parameters. Like @userXxxx notes, you don’t need a transaction just to do a SELECT.

    <?php
    
    class db {
    
        public $connection; //PDO
        public $dbFields; // This is an array of the fields plus VALUES
    
        public function select($where = '1', $params = array(), $limit = '', $fetchStyle = PDO::FETCH_ASSOC) { //fetchArgs, etc
            $fields = implode(', ', $this->dbFields);
    
            //create query
            $query = "SELECT $fields FROM {$this->table} WHERE $where $limit";
    
            //prepare statement
            $stmt = $this->connection->query($query);
    
            $stmt->execute($params);
    
            return $stmt->fetchAll($fetchStyle);
        }
    
        //...
    }
    
    
    $where = 'userId IN(:userId1, :userId2)';
    $params = array(':userId1' => 111, ':userId2' => 2222);
    $db->select($where, $params);
    

    Notes:

    • If you really want, you can add additional method parameters to match up with all the flexibility of PDOStatement::fetchAll.
    • I’m not sure what you mean about $dbFields being “fields plus VALUES”. Can you explain?

    [Edit]

    You might want to take a look at the docs/examples for PDOStatement::execute, since that seemed to be where your confusion was rooted–in particular, the $input_parameters method parameter.

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

Sidebar

Related Questions

I am just two days old in using JMeter . I have been asked
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
i am quite new to iphone development but i have been given a old
I have been trying to compile an old project for my company, which uses
I'm a 15-year-old programmer, and I have been programming for a few years, but
I have been using MVC for a while now and am quite happy with
I've been out of school for a few years now, and just recently started
So I've been using QBASIC64 today... for old school's sake. I was wondering: a)
I have been using .htaccess to redirect URL's on an old website to a
I have been tasked to read in some data from some weird old system.

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.