...

View Full Version : row count in mysql



zodehala
01-13-2008, 07:37 PM
first my codes

$link = mysql_connect("localhost","root","123") or die (mysql_errno()."<br/>". mysql_error());
$db = mysql_select_db("forum",$link) or die (mysql_errno()."<br/>". mysql_error());

i can count rows using mysql console

SELECT COUNT(*) FROM user

and i can count rows using following code


$result = mysql_query("SELECT * FROM user", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

why can not i count rows following ?

[PHP]echo mysql_query("SELECT COUNT(*) FROM user",$link);

its output is

Resource id #4[/QUOTE]

Arnaud
01-13-2008, 08:18 PM
I think COUNT needs a GROUP clause...

zodehala
01-13-2008, 08:48 PM
I think COUNT needs a GROUP clause...
........................

http://img171.imageshack.us/img171/7000/snap1vg6.gif

Inigoesdr
01-13-2008, 09:37 PM
You need to select the value from the first result with mysql_result() (http://php.net/mysql_result):

$result = mysql_query("SELECT COUNT(*) FROM user", $link);
$num_rows = mysql_result($result, 0);
echo "$num_rows Rows\n"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum