Page 1 of 1

hi, can someone help me...

Posted: Wed Dec 10, 2008 1:42 pm
by rjuy
hi!

I have a database with a table name "Student", now what i want is to count the number of student so i used

SELECT COUNT(FirstName) from Student;

If i run this query on the console it output the correct number of student in my database. Now, the problem is i dont know how to output the number in the web.

how to output it using PHP?

my code is :

$sql="SELECT count(FirstName) FROM Student";
$result=mysql_query($sql);
I dont know what next here.....


thanks

-rjuy

Re: hi, can someone help me...

Posted: Wed Dec 10, 2008 1:45 pm
by TheBrandon
http://us3.php.net/mysql-num-rows

Should be able to do it with mysql_num_rows

Re: hi, can someone help me...

Posted: Wed Dec 10, 2008 1:57 pm
by requinix
Use mysql_fetch_row or _array or _assoc to get the first row from the result. You'll get an array from them and you can then get the first field from that.

Code: Select all

$q = mysql_query("SELECT COUNT(FirstName) AS students FROM Student");
 
$row = mysql_fetch_row($q);
echo $row[0]; // or
echo $row["students"];
 
// OR
$row = mysql_fetch_array($q);
echo $row[0];
 
// OR
$row = mysql_fetch_assoc($q);
echo $row["students"];
Doing a SELECT * FROM Students and using mysql_num_rows isn't a good idea. mysql_query will buffer all the results and you don't want that: all you care about is the number of rows in the table, not the data contained.

Re: hi, can someone help me...

Posted: Wed Dec 10, 2008 1:59 pm
by desperado
$total = mysql_num_rows($result)

Re: hi, can someone help me...

Posted: Thu Dec 11, 2008 8:23 am
by rjuy
thanks guys

-rjuy