PHP:如何将数组传递给bind_param

问题描述:

I've been trying to figure this out.

$insertSql = 'INSERT INTO table (id,date,name,numFarts) VALUES (?,?,?,?)';
$values = (1,'0000-00-00 00:00:00','Bob',5);
$bind_param_str = ('issi');
if ($stmt = $db->prepare ($insertSql)) { // $inserSql is a pre-writted sql insert
     $stmt->bind_param($bind_param_str,$values);
     $stmt->execute();
     $stmt->close();
}

This doesn't work, but I can't think of any other way to pass $values into bind_param()

Any ideas?

For any function that you need to pass an array as the argument/s you can use call_user_func_array.

In this example:

array_unshift($values,$bind_param_str);
call_user_func_array(array($stmt,'bind_param'),$values);

Don't ask me why you need array($stmt,'bind_param') instead of $stmt->bind_param. Has something to do with the syntax of -> I'm sure.