Page 1 of 1

How can I get the numbe of the results using count?

Posted: Sat Dec 02, 2006 11:44 am
by ianhull
How can I get the numbe of the results using count?

Thanks

I tried this.

Code: Select all

$countUnits = "SELECT count(*) FROM students_units WHERE username = '$myusername'";
$result = mysql_query($countUnits);
echo $result;

Posted: Sat Dec 02, 2006 11:50 am
by volka
mysql_query -> mysql_fetch_array/_fetch_row/_result like always.

Posted: Sat Dec 02, 2006 11:50 am
by ianhull

Code: Select all

<?php
$countUnits = "SELECT count(*) FROM students_units WHERE username = '$myusername'";
$result = mysql_query($countUnits);
$total = mysql_result($result, 0, 0);
echo $total;
?>
I did it with this.

Re: How can I get the numbe of the results using count?

Posted: Sat Dec 02, 2006 1:33 pm
by timvw
ianhull wrote:How can I get the numbe of the results using count?

Thanks

I tried this.

Code: Select all

$countUnits = "SELECT count(*) FROM students_units WHERE username = '$myusername'";
$result = mysql_query($countUnits);
echo $result;
Since i use mysql_fetch_assoc to retrieve a row from the resultset into an associative array i use an alias for the count(*) column...

SELECT COUNT(student_id) AS count FROM students_unit

And then $row = mysql_fetch_assoc($result); and work with $row['count']....

Re: How can I get the numbe of the results using count?

Posted: Sat Dec 02, 2006 11:29 pm
by dibyendrah
ianhull wrote:How can I get the numbe of the results using count?

Thanks

I tried this.

Code: Select all

$countUnits = "SELECT count(*) FROM students_units WHERE username = '$myusername'";
$result = mysql_query($countUnits);
echo $result;

Code: Select all

$countUnits = "SELECT count(*) as count_students_units FROM students_units WHERE username = '$myusername'";
$result = mysql_query($countUnits);
$result_array = mysql_fetch_array($result);
$count_students_units = $result_array['count_students_units'];

Posted: Sun Dec 03, 2006 1:33 am
by RobertGonzalez
There are two ways. You can test which one is fastest.

Code: Select all

<?php
$sql = "SELECT * FROM students_units WHERE username = '$myusername'";
if (!$result = mysql_query($sql))
{
  die(mysql_error());
}

$row_count = mysql_num_rows($result);
echo $row_count;
?>
or

Code: Select all

<?php
$sql = "SELECT count(*) AS row_count FROM students_units WHERE username = '$myusername'";
if (!$result = mysql_query($sql))
{
  die(mysql_error());
}

$row = mysql_fetch_assoc($result);
$row_count = $row['row_count'];
echo $row_count;
?>