hi, can someone help me...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rjuy
Forum Newbie
Posts: 15
Joined: Thu Apr 17, 2008 4:56 am

hi, can someone help me...

Post 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
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: hi, can someone help me...

Post by TheBrandon »

http://us3.php.net/mysql-num-rows

Should be able to do it with mysql_num_rows
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: hi, can someone help me...

Post 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.
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: hi, can someone help me...

Post by desperado »

$total = mysql_num_rows($result)
rjuy
Forum Newbie
Posts: 15
Joined: Thu Apr 17, 2008 4:56 am

Re: hi, can someone help me...

Post by rjuy »

thanks guys

-rjuy
Post Reply