468,136 Members | 1,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,136 developers. It's quick & easy.

Counting rows in mysql

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
Jul 16 '05 #1
4 18296
On Thu, 17 Jul 2003 23:10:29 GMT, "Matt Schroeder" <wn*********@comcast.net>
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?


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)
Jul 16 '05 #2
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);

http://www.php.net/manual/en/functio...l-num-rows.php

Todd
Jul 16 '05 #3
"T. Relyea" <no****@nospam.com> wrote in message news:<De*******************@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
Jul 16 '05 #4
enigma
9
and what if i have WHERE value NOT NULL
at the end of the query?

i always get "not a valid MySQL result resource" for mysql_result
Sep 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Steven | last post: by
reply views Thread by DavidC | last post: by
7 posts views Thread by Art | last post: by
1 post views Thread by Newmanbt | last post: by
1 post views Thread by whitep8 | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.