tags:

views:

96

answers:

2

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']

  }
+4  A: 

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.

Mike B
How to solve problems like this: Look up the function that is populating $row. In your case, it's mysql_fetch_array(). From the documentation (and the name) you can see that this returns an array. So the question that you're really asking is "How to count elements in an array in PHP?". If you Google that question, you get your answer: count(). So understanding return types is key to making PHP development easier.
sixfoottallrabbit
A: 

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);
?>
Alexander.Plutov