sql_count

The sql_count() function returns the number of rows for a selection resource opened with sql_select().

It accepts 3 parameters:

  1. $res is the resource identifier for a selection,
  2. $serveur,
  3. $option.

It is used as shown below:

$res = sql_select('column', 'table');
if ($res and sql_count($res)>2) {
    // checks to see if there are at least 3 rows in the results!
}

Example

Possible application: display a count of the total number of elements.

if ($res = sql_select('titre', 'spip_rubriques', 'id_parent=0')) {
    $n = sql_count($res);
    $i = 0;
    while ($r = sql_fetch($res)) {
        echo "Section " . ++$i . " / $n : $r[titre]<br />";
        // e.g. Section 3 / 12 : La fleur au vent
    }
}

Author Mark Baber Published : Updated : 01/06/10

Translations : English, français