Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Counting rows in mysql

0 views
Skip to first unread message

Matt Schroeder

unread,
Jul 17, 2003, 7:10:29 PM7/17/03
to
Does anyone know how to count how many rows are in a mysql table? This is
what I have, but it doesn't work right:

<?
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("database",$db);
$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sql);
echo "$result";
?>

This returns "Resource id #3". All it should say is '2'. Can anyone tell me
what I'm doing wrong?

Thanks,
Matt


Andy Hassall

unread,
Jul 17, 2003, 7:17:00 PM7/17/03
to
On Thu, 17 Jul 2003 23:10:29 GMT, "Matt Schroeder" <wnawo...@comcast.net>
wrote:

What makes you think that mysql_query should return 2?

Please read the manual: http://uk.php.net/mysql_query

The next to last paragraph in particular.

"Only for SELECT,SHOW,DESCRIBE or EXPLAIN statements, mysql_query() returns a
new result identifier that you can pass to mysql_fetch_array() and other
functions dealing with result tables."

etc.

--
Andy Hassall (an...@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

T. Relyea

unread,
Jul 17, 2003, 7:24:19 PM7/17/03
to
Matt Schroeder wrote:

You want to use:

mysql_num_rows($result);

http://www.php.net/manual/en/function.mysql-num-rows.php

Todd

R. Rajesh Jeba Anbiah

unread,
Jul 18, 2003, 1:30:01 AM7/18/03
to
"T. Relyea" <nos...@nospam.com> wrote in message news:<DeGRa.26035$G%5.1...@twister.nyroc.rr.com>...

> Matt Schroeder wrote:
>
> > Does anyone know how to count how many rows are in a mysql table? This is
> > what I have, but it doesn't work right:
> >
> > <?
> > $db = mysql_connect("localhost", "username", "password");
> > mysql_select_db("database",$db);
> > $sql = "SELECT COUNT(*) FROM table";
> > $result = mysql_query($sql);
> > echo "$result";
> > ?>
> >
> > This returns "Resource id #3". All it should say is '2'. Can anyone tell me
> > what I'm doing wrong?
> >
> > Thanks,
> > Matt
>
> You want to use:
>
> mysql_num_rows($result);


Use mysql_num_rows($result) when your query is "SELECT * FROM table";

If your query is "SELECT COUNT(*)...", use

$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sql);

$num = mysql_result($result, 0);
echo $num;


---
"If there is a God, he must be a sadist!"
Email: rrjanbiah-at-Y!com

0 new messages