472,805 Members | 966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 18606
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steven | last post by:
Hi there, I have an admin system (Add/Modify/Delete) for two types of records in the database. They have a Parent - Child relationship My problem is that I want to display on the "parent"...
0
by: DavidC | last post by:
Anyone know how to count rows in a table depending on 2 columns, and different values within the second column. Basically i am trying to obtain the resultset example below from the table example...
2
by: Adam | last post by:
In my MYISAM table I have an index (Index_A) on 2 fields (Field_A, Field_B). There are millions of rows in the table. The cardinality of Index_A is 53. I think a query to count the number of rows...
7
by: Art | last post by:
Hi, I'm trying to count rows in each of the tables in an SQL DB. I tried the following ExecuteScalar command: "Select Count(*) from " & mTableName The problem is that one of the tables is...
1
by: Newmanbt | last post by:
I've got the following code testlabel.Text = allquestions.Select().Count.ToString(); GridView1.DataSource = allquestions.Select(); GridView1.DataBind(); testlabel.Text =...
0
by: newtomysql | last post by:
Here is a sample database for my problem: Table t: Num Winner Loser 0 Fred Harry 1 Harry George 2 Fred Eric
1
by: jasone | last post by:
hey, this is what ive got so far: ("SELECT (Select count(*) from tbl_flight_details) + (select count(*) FROM tbl_flight_departures) as grandtotal") i need to count all the records in the...
7
by: crochunter | last post by:
Hi I was trying to count rows and columns in a tab delimited flat file. Like here in example below i should be able to detect automatically the no of rows and columns. So I should get 5 columns...
1
by: whitep8 | last post by:
Hi all, Im at a loss here. There are definitly a lot of emails repeated in the table, but it just wont echo out anything. all help is appreciated <?php session_start(); include...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.