Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Count problem

Options
  • 24-10-2006 6:05pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    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 Posts: 68,317 ✭✭✭✭seamus


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


  • Registered Users Posts: 1,987 ✭✭✭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 Posts: 68,317 ✭✭✭✭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 Posts: 1,987 ✭✭✭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 Posts: 1,987 ✭✭✭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