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
hi, can someone help me...
Moderator: General Moderators
-
TheBrandon
- Forum Commoner
- Posts: 87
- Joined: Tue May 20, 2008 8:55 am
Re: hi, can someone help me...
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.
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.
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"];Re: hi, can someone help me...
$total = mysql_num_rows($result)
Re: hi, can someone help me...
thanks guys
-rjuy
-rjuy