I am using following code to insert multiple records into a table. As you can see the query is in a loop. Every time the loop executes, seperate sql query is run to insert it.
I want a approach where instead of running multiple queries, only single INSERT sql query should run with multiple insert statements.
Though this code of mine is functioning perfectly but I need to optimize it.
$managed_ailments = unserialize($ailments);
foreach($managed_ailments as $ailment_id)
{
$sql = "INSERT INTO ". TABLE_PATIENTS_AILMENTS_RECORDS ."";
$sql .= "(patient_id, ailment_id, datecreated) VALUES";
$sql .= "($patient_id, $ailment_id, now())";
$query = $mysql->query($sql);
}
Here $managed_ailments is a serialized array which i have unserialized. There can be one or more than one values in the array. $mysql->query($sql) is my custom function.
Please help me to convert this code into a single sql query which does it all.
Thanks in advance.
.""at the end of the 4th line? You're appending an empty string.