View Full Version : Count and rows code

12-13-2010, 03:18 AM
What is the explanation or meaning of this code:

$totalarr = count($opname) - 1;


$totalrows = mysql_num_rows($result);
$trows = $totalrows - 1;

Thank you

12-13-2010, 04:57 AM
Count = Counts all elements in an array, or properties in an object.

mysql_num_rows = Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

12-13-2010, 09:03 AM
$totalarr = count($opname) - 1;

$totalarr is most likely refering to an array based on 'arr', once count() has done its business as explained by myfayt, then a -1 is added to the final score combat the fact that in programming indexing starts at 0 not 1. Due to this little confusing fact when you have 5 elements in array you will actually stop on index 4. so you want $totalarr to reflect the correct index as the array has. If you were to try and access the last element of an array using a value from $totalarr without having the -1... you will get an out of bounds error. This will happen because you were asking it to access index 5 when the array only goes index 4.