...

View Full Version : count to array



zodehala
01-14-2012, 04:22 PM
i ma using count($row) to count total variable of mysql but i cant why ?



echo '
<table class="tablo2">
<tr>
<td></td>
<td><div>total :'.count($row).'</div></td>
<td></td>
</tr>
';
$res = mysql_query("SHOW GLOBAL STATUS");
$i=0;
while ($row = mysql_fetch_assoc($res)) {
$i++;
echo '
<tr>
<td><div>'.$i.'</div></td>
<td><div>'.$row['Variable_name'].'</div></td>
<td><div>'.$row['Value'].'</div></td>
</tr>
';

}
echo '
</table>
';

myfayt
01-14-2012, 04:30 PM
$row doesn't exist until later in the script, not sure if that's the reason tho. Usually if you try to fetch something before it's called, it can't find it.

Also using double quotes on your echo would be better, meaning you are using a variable of some form in it.

What value does it show when you run the script?

12k
01-14-2012, 05:53 PM
Using fetch assoc gets the columns, not the values. If you want to count rows from MySQL, you can simply use mysql_num_rows. Example:



$query = mysql_query("SELECT * FROM `table` WHERE `blah`='blah' LIMIT 1", $connection);
$rowCount = mysql_num_rows($query);


(unless you were wanting the total count of the columns)

Then you could use
$rowCount = count(mysql_fetch_assoc($res));

The mysql_fetch_assoc doesn't loop through, so your result would always be 1 in your example. It contains an array of colums. So you would have to use this way to count them

zodehala
01-14-2012, 07:59 PM
but my query isnto "SELECT* FROM" mine is mysql_query("SHOW GLOBAL STATUS")

_Aerospace_Eng_
01-14-2012, 08:31 PM
It doesn't matter. Use mysql_num_rows on your result set.

mysql_num_rows($res);
That will give you the total rows.

zodehala
01-14-2012, 09:41 PM
It doesn't matter. Use mysql_num_rows on your result set.

mysql_num_rows($res);
That will give you the total rows.



Warning: mysql_num_rows() expects parameter 1 to be resource, null given in E:\xampp\htdocs\tser\x.php on line 13

_Aerospace_Eng_
01-15-2012, 01:30 AM
$res needs to happen before you do the count



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum