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 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

Related Questions

I'm learning R and like the language very much because of its flexibility, but
I really like the flexibility of declaring my ViewModel on the DataContext of a
My main concern is not so much flexibility ( of course, that's given like
I have a string like this: my $string = 'Respect,13,201,7,0,0,2,3.70,4,1.01,Responsibility,13,177,29,1,1,2,3.58,4,1.04,Flexibility,13,180,27,0,0,3,3.59,4,1.05,Collaboration,13,194,13,0,0,3,3.65,4,1.04,Reflection,13,187,19,1,0,3,3.62,4,1.05,Commitmentto Learning,13,183,24,0,0,3,3.61,4,1.05,Beliefin Educator Efficacy,13,177,13,0,0,20,3.35,4,1.42,SocialIntelligence,13,184,22,1,0,3,3.61,4,1.05'; How
I'm developing a server application, and would like to harness the flexibility and (possible)
Coming from a javascript background, collections like dictionaries are often implemented as objects because
I want to embed some HTML on my website... I would like that: SEO
Once again, I have a problem for which I would like to shave off
I like how Rails gives me flexibility in naming view files, ie index.erb or
This may sound like a pipe dream, I'm wondering if it's possible. I want

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.