PHP - MySQL prepared statement to INSERT an array

后端 未结 3 891
暖寄归人
暖寄归人 2021-01-15 02:45

I\'m editing a script that is using MySQLi. I need to use prepared statement to insert some values into the db.

My array is in the form of:

$insert =         


        
3条回答
  •  情深已故
    2021-01-15 03:25

    I think this is what you are looking for:

    cols = array_keys($insert);
    $query = "INSERT INTO results (". implode(", ", $cols) .") VALUES (". str_repeat('?', count($insert)) .")";
    $stmt = $mysqli->prepare($query);
    
    call_user_func_array('mysqli_stmt_bind_param', array_merge (array($stmt, str_repeat('s', count($insert))), $insert); 
    

提交回复
热议问题