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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:31:38+00:00 2026-06-12T04:31:38+00:00

Don’t get me wrong PDO is great but what I don’t like about it,

  • 0

Don’t get me wrong PDO is great but what I don’t like about it, is how variables are placed far away from the place they belong inside the SQL statement. Meaning I have a query like:

$stmt = $dbh->prepare("SELECT * FROM users WHERE email = ? AND pass = ?");

The variables that are replacing the ? are always far away some lines below:

$stmt->bindParam(1, $email);
$stmt->bindParam(2, $pass);

If you have a lot of parameters this can get quite ugly. Using :email instead of ? does not make it much better. Sometimes you see the parameters as array in the same methode like:

$db->query("SELECT * FROM users WHERE email = ? AND pass = ?",
   array($email, $pass));

A little bit better but with 10 variables it is still ugly. You practically create a variable ? you only use once. Also code highlighting is not supported for this pseudo variable.

I think it would be nicer to have it like this

$db->prepare("SELECT * FROM user WHERE email = ", $email, " AND pass = ", $pass);

You could even include the parameters of binParam() like this:

$db->prepare_new(
   "SELECT * FROM user WHERE email = ", array($email, PDO::PARAM_STR),
   " AND pass = ", $pass);

I wounder if there is a library that supports this type of style. Do you know one?

  • 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-12T04:31:39+00:00Added an answer on June 12, 2026 at 4:31 am

    If always every even parameter will be parameter you can do it like this:

    class MyPDO extends PDO {
        public function prepareQuery() {
            $query_str = "";
            $params = array();
            foreach(func_get_args() as $key => $param) {
                if( $key % 2 == 0 ) {
                    $query_str .= $param;
                }
                else {
                    $params[] = $param;
                    $query_str .= ' ? ';
                }
            }
    
            $prepared = $this->prepare($query_str);
            foreach( $params as $key => $param ) {
                $prepared->bindParam( $key+1, $param );
            }
            return $prepared;
        }
    }
    

    and then you can use it as you wanted:

    $db = new MyPDO( .. );
    $db->prepareQuery("SELECT * FROM user WHERE email = ", $email, " AND pass = ", $pass);
    

    PS: not tested – just a concept

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

Sidebar

Related Questions

Don't get me wrong - I love Smalltalk, but... To me, the Squeak interface
Don't get me wrong, I want them to get saved. But I always thought
don't know if the title describes anything about what I'm trying to say but
Don't know how to explain it better but i'm trying to get a response
Don't know if this is the right place to ask this, but I will
Don't know where else to ask, but from one day to the other my
Don't ask me how but I managed to get accidentally the following remote branches
Don't know if it matters, but the msg.d is about 300 rows long. I
Don't need to do this right now but thinking about the future... What would
Don't know if I'm missing something here. I'd like to run multiple global variables

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.