user342391
user342391

Reputation: 7827

How to count fields in row?

How can I count how many fields there are in this row:

$row['number']
while ($row = mysql_fetch_array($result))
{
    echo '
    <td class="alt">'.$row['number'].'</td>
    $number = $row['number']
}

Upvotes: 0

Views: 1116

Answers (2)

Alex Pliutau
Alex Pliutau

Reputation: 21957

Try to use mysql_num_fields(). Example:

<?php
$result = mysql_query("SELECT `field1`,`field2` FROM `table`");
/* returns 2 because field1, field2 === two fields */
echo mysql_num_fields($result);
?>

Upvotes: 0

Mike B
Mike B

Reputation: 32155

It could depend on how you're populating $row. If you use mysql_fetch_assoc() or mysql_fetch_row() you can just use count($row). However, if you use mysql_fetch_array() you'll need to divide by 2 since it returns both enumerated and associative values.

There are countless other methods of populating $row. It's all merely speculation without having more information.

Upvotes: 4

Related Questions