Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: count to array

  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    $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?
    Last edited by myfayt; 01-14-2012 at 03:33 PM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    but my query isnto "SELECT* FROM" mine is mysql_query("SHOW GLOBAL STATUS")

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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!||||

  • #6
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    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 

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    $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!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •