php - Inserting values : prepared statement or multiple values query? -
in opinion , performance point of view, best solution insert multiple values in table ?
1 - prepared statement :
$usersid = users::getallid($this->sql); $prep = $this->sql->prepare('insert notification_actualites (iduser,idnews) values(:idu,:idn)'); foreach($usersid $idu) { $prep->execute(array( ':idu' => $idu, ':idn' => $idn )); }
2 - or multiple values query :
$usersid = users::getallid(); $values=''; foreach($usersid $id) { $values.='(\''.$id.'\','.$idactu.'),'; } $values = substr($values,0,strlen($values)-1); $this->sql->query('insert notification_actualites values'.$values);
the security aspect not problem here , in both case, code adapted prevent sql injection.
a well-argued answer appreciated :)
thanks
i prefer later method. every database request has sent database server , receive results - takes time, if database server running on different machine.
time_to_prepare_query + time_sending_query + time_to_executing_query + time_receiving_results + time_processing_results
when using second method, don't have count time_sending_query , time_receiving_results n times (n being number of rows sent db).
on other hand, have less control on resolving possible individual errors might arise (like duplicate keys (although on duplicate key
can save trouble), bad values etc).
the choice of method - - depend on application.
Comments
Post a Comment