Coding with SPIP 2.1
SPIP 2.1 Technical Documentation

> > > > sql_insertq_multi

sql_insertq_multi

The sql_insertq_multi() function is used to insert, in one single action, several elements with identical schemas into a database table. If the database manager port allows it, it will then use a single SQL command to implement the insert. More specifically, a single SQL command for each batch of 100 elements is used in order to avoid memory congestion.

The function has the same 5 parameters as sql_insertq() , but the second parameter for this function is a table of a table of pairs, and not just directly a table of pairs:

  1. $table is the name of the SQL table,
  2. $couples is a table of associative tables of name / value pairs,
  3. $desc,
  4. $serveur,
  5. $option.

The columns used in this command absolutely must be the same set for all of the inserts. The command is used as shown below:

  1. $id = sql_insertq_multi('table', array(
  2. array('column' => 'value'),
  3. array('column' => 'value2'),
  4. array('column' => 'value3'),
  5. );

Download

Example

Searches made using SPIP use the spip_resultats table to store some elements used as a cache, by taking care to use the table for the SQL connection. $tab_couples contains all of the data to be inserted:

  1. // insert the results into the results cache table
  2. if (count($points)){
  3. $tab_couples = array();
  4. foreach ($points as $id => $p){
  5. $tab_couples[] = array(
  6. 'recherche' => $hash,
  7. 'id' => $id,
  8. 'points' => $p['score']
  9. );
  10. }
  11. sql_insertq_multi('spip_resultats', $tab_couples, array(), $serveur);
  12. }

Download

The "Polyhierarchie" plugin also uses this function for inserting the list of sections just recently linked to a given object:

  1. $ins = array();
  2. foreach($id_parents as $p){
  3. if ($p) {
  4. $ins[] = array(
  5. 'id_parent' => $p,
  6. 'id_objet' => $id_objet,
  7. 'objet' => $objet);
  8. }
  9. if (count($ins)) {
  10. sql_insertq_multi("spip_rubriques_liens", $ins, "", $serveur);
  11. }

Download

  • Author :
  • Published :
  • Updated : 02/06/10
  • Translations : English, français