I am currently looping through an array to update one row at a time, for example:
for ($i = 0; $i < count($users); $i++) {
$query = "UPDATE users SET name = :name WHERE id = :id";
$statement = $pdo_conn->prepare($query);
$result = $statement->execute('name' => $users[$i]['name'], 'id' => $users[$i]['id']);
}
Is there any way to make this 1 query, or optimize in any way?
Use foreach() and iterate $users array, bind values and execute statements inside the array
$stmt = $pdo->prepare('UPDATE users SET name = :name WHERE id = :id');
foreach($users as $user)
{
$stmt->bindValue(':name', $user['name']);
$stmt->bindValue(':id', $user['id']);
$stmt->execute();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.