Jump to content

Recommended Posts

I'm working with prepared statements and need an array to be formatted in a certain way automatically.

 

Lets say I've got $testarray, with varying amounts of parameters.

 

I need to get this displayed as:

 

mysqli_stmt_bind_param($stmt, 'sssss', $testarray[1],$testarray[2],$testarray[3],$testarray[4],$testarray[5]);

 

How can I do this? The following will not work:

 

$foo = $testarray[1],$testarray[2],$testarray[3],$testarray[4],$testarray[5];

mysqli_stmt_bind_param($stmt, 'sssss', $foo);

 

Any ideas?

single quotes do not evaluate you mean, and yes I already know that.. my aim was to get it to be literal

 

xx,xx,xx,xx

 

since they are variables I did not want the variables evaluating.. then in the eval I evaluate the variable once, so that it places the string

 

$xx,$xx,$xx,$xx

 

inside the function, then the eval() will evaluate the function with the now in place variables

Unfortunately the eval() did not work, so I continued to look around and actually stumbled on the answer:

 

$types = str_repeat('s', count($bind_params));
array_unshift($bind_params, $types);
array_unshift($bind_params, $stmt);
call_user_func_array('mysqli_stmt_bind_param', $bind_params); 

 

(where $bind_params is the array of data)

 

Works great and is very flexible!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.