FYI. ended up going with PDO solution as this was simpler.
I’m trying to add a single method to handle all queries to the database. I want the queries to use parameter binding. How do I handle a variable amount of function parameters in mysqli_stmt_bind_param()?
This post here led me to understand the pros of parameter binding.
Here is my example code..where I am currently stuck at is marked.
INPUT PARAMETERS
$query = "INSERT INTO b0 VALUES (?, ?, ?)"
$par_arr = {'bookmark', 'http://www.bookmark.com', 'tag'}
PROTOTYPE CODE
protected static function query($query, $par_arr)
{
if($statement=mysqli_prepare(one::$db, $query)
{
mysqli_stmt_bind_param($statement, "s", ...variable amount of parameters...);<----how should this be handled?
...
Update 2: If you experience any further problems with this code, then you should probably follow this advice and use PDO instead.
This is how you should be using
call_user_func_array[docs]:where
$typesis a string indicating the type of each value, as described in themysqli_stmt_bind_paramdocumentation (call_user_func_arrayis even mentioned there).Update: It seems it is not that easy after all, and you have to create references to the values first:
No it’s not. The first parameter is of type
callback, and the documentation says (emphasis mine):Next remark:
Have you had a look at the examples? Each element of the array you pass to
call_user_func_arraywill be passed as argument to the function you specify. Arrays are the only way to have a collection of values of variable size.