Request your help on how to insert a PHP array of type below into Mysql with single query by avoiding the foreach loop as below query.
What would be the performance gain in we use a single query to insert vs the below query(foreach).
PHP Array
(
[0] => Array
(
[Name] => NNN1
[Type] => TTT1
)
[1] => Array
(
[Name] => NNN2
[Type] => TTT2
)
Query:
foreach ($array as $i) {
$na = $item["Name"];
$ty = $item["Type"];
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$dc = $db->prepare("INSERT INTO allocation (`Name`, `Type`) VALUES (?, ?) ON DUPLICATE KEY UPDATE Type = IF(Type != VALUES(Type), VALUES(Type), '$ty')");
$dc->bind_param("ss", $na, $ty);
$dc->execute();
$dc->execute();
$dc->close();
}
foreach, you can movenew mysqli()and$db->preparebefore the loop. And remove the second$dc->execute()Type = IF(Type != VALUES(Type), VALUES(Type), '$ty')doesn't make any sense asVALUES(Type)will be the same as$ty, so you may as well just writeType = VALUES(Type)