NYCPHP Meetup

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

Jonathan Hendler hendler at simmons.edu
Wed Feb 22 01:15:17 EST 2006


For my two cents, I've done something like this using ADODB. 
But maybe you loose the speed benifits by using ADODB instead of a 
straight call.
Does anyone know the difference of using repeated prepared statements 
and a systax where you could have a giant insert statement:

INSERT INTO table(c1,c2)
VALUES (v1.1,v2.1),..., (v1.n,v2.n)

I also found this resource useful:
http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html

Andrew Yochum wrote:

>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);
>
>HTH,
>Andrew
>
>  
>




More information about the talk mailing list