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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

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

Post 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;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mysql_query -> mysql_fetch_array/_fetch_row/_result like always.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

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

Post 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']....
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

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

Post 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'];
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
?>
Post Reply