PDA

View Full Version : how to count records on php



hardinera
03-08-2010, 07:07 AM
//DISPLAYING CONTENT!!
echo "----------display content <br>";
$select = mysql_query("SELECT * FROM guest_info where room_type = 'single room' and
check_in >= '0000-00-00'");

while($row = mysql_fetch_array($select)){

echo $row['guest_id'] . "<br>" . "<br>";

}
//COUNTING CONTENT
echo "----------counting <br>";


$total_results = mysql_query("SELECT count('guest_id') FROM guest_info where room_type = 'single room' and check_in >= '0000-00-00'");

$row = mysql_fetch_array($total_results);

$result = count($row);

echo $result;


here is my code.. the displaying part is doing fine.. but its not counting

anybody can help me? thanks:p

Taylor_1978
03-08-2010, 08:05 AM
I don't know much about the 'count' in a mysql query - but why not just do this:



$total_results = mysql_query("SELECT 'guest_id' FROM guest_info where room_type = 'single room' and check_in >= '0000-00-00'");

$result = mysql_num_rows($total_results);

echo $result;

Taylor_1978
03-08-2010, 08:06 AM
Actually - the other solution is:



//DISPLAYING CONTENT!!
echo "----------display content <br>";
$select = mysql_query("SELECT * FROM guest_info where room_type = 'single room' and
check_in >= '0000-00-00'");

while($row = mysql_fetch_array($select)){
$count++;
echo $row['guest_id'] . "<br>" . "<br>";

}
//COUNTING CONTENT
echo "----------counting <br>";
echo $count;

Dormilich
03-08-2010, 02:01 PM
counting is best done by the DB itself:`

SELECT COUNT(*) AS `number` FROM …
recommendation, don’t use * but the field names you actually need. this way you get the array keys from looking at the sole query string.