Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2012, 03:22 PM   PM User | #1
zodehala
Regular Coder

 
Join Date: Dec 2007
Posts: 269
Thanks: 28
Thanked 0 Times in 0 Posts
zodehala can only hope to improve
count to array

i ma using count($row) to count total variable of mysql but i cant why ?

Code:
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> 
';
zodehala is offline   Reply With Quote
Old 01-14-2012, 03:30 PM   PM User | #2
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
$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?
__________________
Been a sign maker for 5 years. My business:
American Made Signs

Last edited by myfayt; 01-14-2012 at 03:33 PM..
myfayt is offline   Reply With Quote
Old 01-14-2012, 04:53 PM   PM User | #3
12k
New Coder

 
Join Date: Jan 2012
Posts: 29
Thanks: 0
Thanked 6 Times in 6 Posts
12k is an unknown quantity at this point
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:

PHP Code:
$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

Last edited by 12k; 01-14-2012 at 04:56 PM..
12k is offline   Reply With Quote
Old 01-14-2012, 06:59 PM   PM User | #4
zodehala
Regular Coder

 
Join Date: Dec 2007
Posts: 269
Thanks: 28
Thanked 0 Times in 0 Posts
zodehala can only hope to improve
but my query isnto "SELECT* FROM" mine is mysql_query("SHOW GLOBAL STATUS")
zodehala is offline   Reply With Quote
Old 01-14-2012, 07:31 PM   PM User | #5
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
It doesn't matter. Use mysql_num_rows on your result set.
PHP Code:
mysql_num_rows($res); 
That will give you the total rows.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 01-14-2012, 08:41 PM   PM User | #6
zodehala
Regular Coder

 
Join Date: Dec 2007
Posts: 269
Thanks: 28
Thanked 0 Times in 0 Posts
zodehala can only hope to improve
Quote:
Originally Posted by _Aerospace_Eng_ View Post
It doesn't matter. Use mysql_num_rows on your result set.
PHP Code:
mysql_num_rows($res); 
That will give you the total rows.
PHP Code:
Warningmysql_num_rows() expects parameter 1 to be resourcenull given in E:xampphtdocstserx.php on line 13 
zodehala is offline   Reply With Quote
Old 01-15-2012, 12:30 AM   PM User | #7
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
$res needs to happen before you do the count
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:24 AM.


Advertisement
Log in to turn off these ads.