Tuesday, 6 September 2016

php - Prepared multiple Insert mysqli




Please read the full question before closing it :)
Im looking for a prepared statement with mysqli (Important, not PDO, because I can't use it and can't transfer some PDO code to mysqli.), where i can insert on long query with a lot of values (about 2000). But the query has to be prepared.



So i started like that:

$array = array("a1", "a2", "a3","a5", "a7", "a5","a9", "a32", "a3", "a4"); 
// AND SO ON UP TO 2000
$type = "s";
$end = count($array);


$query = "INSERT INTO table (value) VALUES (?)";
for ($i = 0; $i <= $end - 1; $i++)
{
$query .= ", (?)";
$type .= "s";
}

$stmt = $conn->prepare($query);
$stmt->bind_param("$type", /* PROBLEM */); // HERE IS THE PROBLEM!!!
$stmt->execute();

$stmt->close();




But now my problem, how can I bind the variables in "bind_param" dynamicaly?
Please dont show me anything like for-loops on "execute", because this is too sloww for 2000 inserts :).

I mean something like



$allvalues = "";
foreach ($array as $value)

{
$allvalues .= "$value ";
}
$stmt->bind_param("$type", $allvalues);


But of course, I cant bind that.


Answer



Use call_user_func_array to call the function using an array.




Or just pass all of the variables through to execute, then you do not even need to use bind_param


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...