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:
-
$res
is the resource generated by an sql_select(), -
$serveur
, -
$option
.
This function is used in strict conjunction with sql_select()
, often used in the following manner:
if ($res = sql_select('column', 'table')) {
while ($r = sql_fetch($res)) {
// use the results with $r['column']
}
}
Example
List the articles proposed for publication:
$result = sql_select("id_article, id_rubrique, titre, statut", "spip_articles", "statut = 'prop'", "", "date DESC");
while ($row = sql_fetch($result)) {
$id_article=$row['id_article'];
if (autoriser('voir', 'article', $id_article)) {
// actions
}
}
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:
function action_supprimer_message() {
$securiser_action = charger_fonction('securiser_action', 'inc');
$id_message = $securiser_action();
// Check if we have any documents
if ($docs = sql_select('id_document', 'spip_documents_liens', 'id_objet=' . intval($id_message) . ' AND objet="message"')) {
include_spip('action/documenter');
while ($id_doc = sql_fetch($docs)) {
supprimer_lien_document($id_doc['id_document'], "message", $id_message);
}
}
sql_delete("spip_messages", "id_message=" . sql_quote($id_message));
sql_delete("spip_auteurs_messages", "id_message=" . sql_quote($id_message));
}
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:
// Set the counters to zero
sql_updateq('spip_rubriques', array(
'date_tmp' => '0000-00-00 00:00:00',
'statut_tmp' => 'prive'));
// [...]
// Publish and date the sections which have a published *document*
$r = sql_select(
array(
"rub.id_rubrique AS id",
"max(fille.date) AS date_h"),
array(
"spip_rubriques AS rub",
"spip_documents AS fille",
"spip_documents_liens AS lien"),
array(
"rub.id_rubrique = lien.id_objet",
"lien.objet='rubrique'",
"lien.id_document=fille.id_document",
"rub.date_tmp <= fille.date",
"fille.mode='document'", "rub.id_rubrique"));
while ($row = sql_fetch($r)) {
sql_updateq('spip_rubriques',
array(
'statut_tmp'=>'publie',
'date_tmp'=>$row['date_h']),
"id_rubrique=" . $row['id']);
}
// [...]
// Save the modifications
sql_update('spip_rubriques', array(
'date'=>'date_tmp',
'statut'=>'statut_tmp'));