skyer2000 Posted May 10, 2009 Share Posted May 10, 2009 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? Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/ Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 $foo = $testarray[1],$testarray[2],$testarray[3],$testarray[4],$testarray[5]; Syntax error Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831320 Share on other sites More sharing options...
RussellReal Posted May 11, 2009 Share Posted May 11, 2009 I'm sure u can do something like: $foo = '$testarray[1],$testarray[2],$testarray[3],$testarray[4],$testarray[5]'; eval('mysqli_stmt_bind_param($stmt, \'sssss\', '.$foo.')'); Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831336 Share on other sites More sharing options...
hchsk Posted May 11, 2009 Share Posted May 11, 2009 i think ken meant to look up concatenating strings? Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831337 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 RussellReal, using single quotes fail. Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831345 Share on other sites More sharing options...
RussellReal Posted May 11, 2009 Share Posted May 11, 2009 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 Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831423 Share on other sites More sharing options...
skyer2000 Posted May 11, 2009 Author Share Posted May 11, 2009 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! Link to comment https://forums.phpfreaks.com/topic/157634-solved-displaying-an-array-in-a-prepared-statement/#findComment-831550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.