Coding with SPIP 3.0
SPIP3.0 Technical Documentation

> > > > sql_fetch

sql_fetch

The sql_fetch() function returns a row, in the form of an associative array, from the results of a selection. It returns false if there are no more rows to be retrieved.

It accepts 3 parameters, only the first of which is mandatory:

  1. $res is the resource generated by an sql_select(),
  2. $serveur,
  3. $option.

This function is used in strict conjunction with sql_select(), often used in the following manner:

  1. if ($res = sql_select('column', 'table')) {
  2. while ($r = sql_fetch($res)) {
  3. // use the results with $r['column']
  4. }
  5. }

Download

Example

List the articles proposed for publication:

  1. $result = sql_select("id_article, id_rubrique, titre, statut", "spip_articles", "statut = 'prop'", "", "date DESC");
  2. while ($row = sql_fetch($result)) {
  3. $id_article=$row['id_article'];
  4. if (autoriser('voir', 'article', $id_article)) {
  5. // actions
  6. }
  7. }

Download

The "Contact avancé" plugin can save messages in the spip_messages table. When one of these messages is deleted, it also deletes any documents that may be linked to it:

  1. function action_supprimer_message() {
  2. $securiser_action = charger_fonction('securiser_action', 'inc');
  3. $id_message = $securiser_action();
  4. // Check if we have any documents
  5. if ($docs = sql_select('id_document', 'spip_documents_liens', 'id_objet=' . intval($id_message) . ' AND objet="message"')) {
  6. include_spip('action/documenter');
  7. while ($id_doc = sql_fetch($docs)) {
  8. supprimer_lien_document($id_doc['id_document'], "message", $id_message);
  9. }
  10. }
  11. sql_delete("spip_messages", "id_message=" . sql_quote($id_message));
  12. sql_delete("spip_auteurs_messages", "id_message=" . sql_quote($id_message));
  13. }

Download

The calculer_rubriques_publiees() function within ecrire/inc/rubriques.php is used to recalculate the statuses and dates for sections in order to find out which have the status of "publié" (published). Within the function, a code segment selects the sections which have published documents (and therefore so does the section) and assigns a temporary column for the new status and new date. Once the updates are completed, the temporary column is saved into the real column:

  1. // Set the counters to zero
  2. sql_updateq('spip_rubriques', array(
  3. 'date_tmp' => '0000-00-00 00:00:00',
  4. 'statut_tmp' => 'prive'));
  5. // [...]
  6. // Publish and date the sections which have a published *document*
  7. $r = sql_select(
  8. "rub.id_rubrique AS id",
  9. "max(fille.date) AS date_h"),
  10. "spip_rubriques AS rub",
  11. "spip_documents AS fille",
  12. "spip_documents_liens AS lien"),
  13. "rub.id_rubrique = lien.id_objet",
  14. "lien.objet='rubrique'",
  15. "lien.id_document=fille.id_document",
  16. "rub.date_tmp <= fille.date",
  17. "fille.mode='document'", "rub.id_rubrique"));
  18. while ($row = sql_fetch($r)) {
  19. sql_updateq('spip_rubriques',
  20. 'statut_tmp'=>'publie',
  21. 'date_tmp'=>$row['date_h']),
  22. "id_rubrique=" . $row['id']);
  23. }
  24. // [...]
  25. // Save the modifications
  26. sql_update('spip_rubriques', array(
  27. 'date'=>'date_tmp',
  28. 'statut'=>'statut_tmp'));

Download

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