[nycphp-talk] Calling mysqli_stmt_bind_param with arbitrary number of parameters?

Andrew Yochum andrew at
Tue Feb 21 19:57:27 EST 2006

Daniel Krook wrote:
> Hello all,
> I'm trying to send parameters to a prepared statement using the mysqli 
> extension.
> The manual shows that it is used like this:
>   $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, 
> ?)");
>   $stmt->bind_param('sssd', $code, $language, $official, $percent);
> Where bind_param can take any number of values like so:
>   $stmt->bind_param('s', $foo);
>   $stmt->bind_param('si', $foo, $bar);
>   $stmt->bind_param('sid', $foo, $bar, $baz); 
> Unfortunately, I can't be sure of the number of placeholders in the SQL 
> statement I am sending, and don't know how many corresponding parameters 
> are in an array I pass to my function.  I would like to replicate this 
> functionality that I use in the Unified ODBC extension:
>   $stmt = odbc_prepare($db, $sql);
>   $result = odbc_execute($stmt, $params); 
> With bind_param it would seem I'd have to construct the bind_param call 
> manually and then eval() it to get the same flexibility.  This is what I'd 
> like to be able to do:
>   $stmt->bind_param($aSeriesOfLettersDependingOnParamsSize, $params);
> This would seem like something I'd need to do but haven't been able to 
> figure out how to pull it off:
>   eval(sprintf('$stmt->bind_param("sss...", %s, %s, %s, ...);') $params);
> Is there a solution to this that anyone has run into?  Am I overlooking 
> something really obvious (anytime eval() comes into play I get that 
> feeling...)

I know that feeling. :)

How about using call_user_func_array()?

Something like:
	array_unshift('sql ? ? ?', $params);
	call_user_func_array(array($stmt, 'bind_param'), $params);


Andrew Yochum
andrew at

More information about the talk mailing list