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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:41:31+00:00 2026-05-10T18:41:31+00:00

I like the flexibility of Dynamic SQL and I like the security + improved

  • 0

I like the flexibility of Dynamic SQL and I like the security + improved performance of Prepared Statements. So what I really want is Dynamic Prepared Statements, which is troublesome to make because bind_param and bind_result accept ‘fixed’ number of arguments. So I made use of an eval() statement to get around this problem. But I get the feeling this is a bad idea. Here’s example code of what I mean

// array of WHERE conditions $param = array('customer_id'=>1, 'qty'=>'2'); $stmt = $mysqli->stmt_init();  $types = ''; $bindParam = array(); $where = ''; $count = 0;  // build the dynamic sql and param bind conditions foreach($param as $key=>$val) {     $types .= 'i';     $bindParam[] = '$p'.$count.'=$param[''.$key.'']';      $where .= '$key = ? AND ';     $count++; }  // prepare the query -- SELECT * FROM t1 WHERE customer_id = ? AND qty = ? $sql = 'SELECT * FROM t1 WHERE '.substr($where, 0, strlen($where)-4); $stmt->prepare($sql);  // assemble the bind_param command $command = '$stmt->bind_param($types, '.implode(', ', $bindParam).');';  // evaluate the command -- $stmt->bind_param($types,$p0=$param['customer_id'],$p1=$param['qty']); eval($command); 

Is that last eval() statement a bad idea? I tried to avoid code injection by encapsulating values behind the variable name $param.

Does anyone have an opinion or other suggestions? Are there issues I need to be aware of?

  • 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. 2026-05-10T18:41:31+00:00Added an answer on May 10, 2026 at 6:41 pm

    I think it is dangerous to use eval() here.

    Try this:

    • iterate the params array to build the SQL string with question marks 'SELECT * FROM t1 WHERE p1 = ? AND p2 = ?'
    • call prepare() on that
    • use call_user_func_array() to make the call to bind_param(), passing in the dynamic params array.

    The code:

    call_user_func_array(array($stmt, 'bind_param'), array($types)+$param); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer EDIT: Besides the limited number of files that Java can… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer Gettext is the way to go, in your example you… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer I suspect that some editor inserts those when the file… May 12, 2026 at 1:03 am

Related Questions

In an Oracle 10g environment, I have a statement that needs to be executed
I am building an application where most of the HTML is built using javascript.
I've been working with the xml.linq classes in .net recently. But may need to
I can't seem to get acceptable performance from FullText Catalogs. We have situations where

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.