The sql_countsel()
functions returns the number of rows for a desired selection. It is more-or-less a short way of writing sql_select('COUNT(*)', ...)
.
It accepts the same arguments as sql_select()
except for the first (normally the columns):
-
$from
, -
$where
, -
$groupby
, -
$having
, -
$serveur
, -
$option
.
It is used as shown in this example:
$nomber = sql_countsel("table");
Example
Count the number of keywords in a given keyword group:
$groupe = sql_countsel("spip_mots", "id_groupe=$id_groupe");
Return false
if a section has any articles NOT in the trash:
if (sql_countsel('spip_articles', array(
"id_rubrique=$id_rubrique",
"statut <> 'poubelle'"
))) {
return false;
}
If the spip_notations_objets
table in the "Notations" table does not yet have any entry for the object identifier specified, then perform a database insert, otherwise perform an update:
// Update or insert?
if (!sql_countsel("spip_notations_objets", array(
"objet=" . sql_quote($objet),
"id_objet=" . sql_quote($id_objet),
))) {
// Insert a record for the object notation
sql_insertq("spip_notations_objets", ...);
// ...
} else {
// Update if there already is a record
sql_updateq("spip_notations_objets", ...);
// ...
}