-1

What im trying to do is to create different insert statements in for loop and execute them while in loop. Is that possible?

Here's simplified code:

$mysqli = new mysqli("localhost", "user", "password", "database");

for($i=1; $i<10; $i++){

    // $query string is created through code, so 
    // INSERT statement is varying after each loop.
    // Lets say, in another step $query will be "INSERT INTO table (row4,row5) VALUES (?,?)";
    // $params will be array("value4","value5");

    $query = "INSERT INTO table (row1,row2,row3) VALUES (?,?,?)";
    $param_type = "sss";
    $params = array("value1","value2","value2");

    $insert_stmt = $mysqli->prepare($query);
    array_unshift($params, $param_type);
    call_user_func_array(array($insert_stmt, 'bind_param'), refValues($params));
    $insert_stmt->execute();
    $insert_stmt->close();

}

What i get if i run this code is only one inserted row and warning "call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in..."

So my question is: how to prepare and insert different querys and parameters through for loop?

woopata
  • 875
  • 5
  • 17
  • 29
  • 1
    -1 for posting "simplified code" which makes whole question pointless. – Your Common Sense Apr 21 '13 at 12:38
  • there's only refValues function not included and i excluded code for building different query's in each loop. I think its not important to show how i build querys. – woopata Apr 21 '13 at 12:41
  • Though error message is pretty clear. Which makes this question duplicate of http://stackoverflow.com/questions/15447133/mysqli-update-throwing-call-to-a-member-function-bind-param-error – Your Common Sense Apr 21 '13 at 12:50

1 Answers1

0

It is possible and even easy to make.
The only thing you need for this is PDO:

$queries = array('insert 1', 'insert 2', ...);
$params  = array(array(...),array(...), ...);

foreach ($queries as $i => $sql) {
    $stm = $pdo->prepare($sql);
    $stm->execute($params[$i]);
}

that's all

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345