Mysqli bind with an array of values

7

I have a code in PDO and I need to convert this code to mysqli, at this moment I have something like that:

$sql = "SELECT * from tabela where nome = ? AND idade = ? AND outro = ?";

$stmt = $core->conn->prepare($sql);

$bindArray = array( $_POST['nome'], $_POST['idade'], $_POST['outro'] );

$stmt->execute($bindArray);

The problem is in the $ bindArray, in the PDO in the function execute step the $ bindArray variable and it automatically binds the array, in mysqli I am not able to.

What could be wrong or can not do the same with mysqli?

    
asked by anonymous 04.12.2015 / 12:56

1 answer

2

Currently it is not possible to pass an array to execute () of MySQLi in the same way as works the PDO execute () , pass values also directly via # is not possible, oddly enough function returns generate a warning and work.

The solution for versions prior to PHP5.6 is to use call_user_func_array() as the responses on the links show.

How do I pass dynamic parameters in a preparedStatment? and Select in MySQL with an array

Select in MySQL with an array

Approach with PHP5.6

With PHP 5.6 and the help of ... (unpacking arguments) it is possible to pass an array to bind_param() simply.

$valores = array('Doge', '[email protected]', 'wow value');
$stmt = $mysqli->prepare('INSERT INTO t (c1,c2,c3) VALUES(?,?,?)');
$stmt->bind_param('sss', ...$valores);
if(!$stmt->execute()){
     echo $stmt->error;
}
    
04.12.2015 / 13:23