Optimize consecutive sql statements

0

In a PHP code you have a series of consecutive UPDATES (could be more)

$sql= "UPDATE SENTENCIA X";
$sth = $BD->prepare($sql);
$sth->execute();

$sql= "UPDATE SENTENCIA Y";
$sth = $BD->prepare($sql);
$sth->execute();

$sql= "UPDATE SENTENCIA Z";
$sth = $BD->prepare($sql);
$sth->execute();

Is there any way to optimize the process of this execution and what would that form be?

    
asked by Piropeator 27.04.2017 в 18:50
source

1 answer

1

If it is the same sentence but only the values that it receives change, the most reasonable thing would be to do something like this:

<?php
/* Se asume la conexión en $BD */

// Creas la consulta
$sql= "UPDATE tabla SET campo=:valor WHERE pk=:pk";

// Preparas una vez
$sth = $BD->prepare($sql);

// Ejecutas tantas como necesites
$sth->execute(array(':valor' => 'valor_1', ':pk' => 'pk_1'));
$sth->execute(array(':valor' => 'valor_2', ':pk' => 'pk_2'));
$sth->execute(array(':valor' => 'valor_3', ':pk' => 'pk_3'));
?>
    
answered by 29.04.2017 в 14:41