Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Count problem

  • 24-10-2006 06:05PM
    #1
    Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭


    Below is the function i have, basicly it will count the first record in the DB table but no more, all im trying to do it return a number of how many rows are in the table 'hl', can anyone help?
    function count_hl()
    {
    	$sql = 'SELECT COUNT(id) FROM hl';
    	$data = mysql_query($sql);
    	$result = mysql_fetch_row($data);
    	
    	return $result;
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Weird. Have you tried count(*) instead of count(id). There is more than one row in the DB right? :)


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Ye, there is more then one row in the table and i have tried (*) but no luck, still wont return the count number!?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Can you post up the code in which you use this function?

    Try this
    function count_hl()
    {
    	$sql = 'SELECT COUNT(id) as num_rows FROM hl';
    	$data = mysql_query($sql);
    	$result = mysql_fetch_array($data);
    	
    	return $result['num_rows'];
    }
    


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    This is how its being called:
    <table width="95%" border="0" cellspacing="0" cellpadding="0">
    	<tr>
    		<td colspan="2" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:15px;">
    			<u>Topic Name</u><br/>
    			&nbsp;
    		</td>
    	</tr>
    	<?php
    	$rows = count_hl();
    	for($counter = 1; $counter <= $rows; $counter++)
    	{
    		echo'
    		<tr>
    			<td width="10" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;">
    				>>
    			</td>
    			<td width="100%" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;">
    				&nbsp;<a href="topic_desc.php?id='.$counter.'">';echo name_hl($counter); echo'</a>';
    		echo'
    			</td>
    		</tr>';
    	}
    ?>
    </table>
    


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    	$sql = "SELECT COUNT(id) FROM table"; 
    	$result = mysql_query($sql);
    	$row = mysql_fetch_array($result);
    	
    	echo ($row['COUNT(id)']);
    

    Simple example, should be easy for you to work up into a function from there.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Nice one lads, got it working.

    Appreciated!


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Ziycon wrote:
    Below is the function i have, basicly it will count the first record in the DB table but no more, all im trying to do it return a number of how many rows are in the table 'hl', can anyone help?
    You can also just add the bit in red below:
    function count_hl()
    {
    	$sql = 'SELECT COUNT(id) FROM hl';
    	$data = mysql_query($sql);
    	$result = mysql_fetch_row($data);
    	
    	return $result[COLOR="Red"][0][/COLOR];
    }
    


Advertisement