473,612 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($sq l);
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 18661
On Thu, 17 Jul 2003 23:10:29 GMT, "Matt Schroeder" <wn*********@co mcast.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_d b("database",$d b);
$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sq l);
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,DES CRIBE or EXPLAIN statements, mysql_query() returns a
new result identifier that you can pass to mysql_fetch_arr ay() 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($sq l);
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******* ************@tw ister.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($sq l);
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($sq l);
$num = mysql_result($r esult, 0);
echo $num;
---
"If there is a God, he must be a sadist!"
Email: rrjanbiah-at-Y!com
Jul 16 '05 #4
enigma
9 New Member
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
1729
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" record whether the child exists or not . The records are found in two different tables, but have the same Primary Key.
0
1437
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 below TABLE example: Date | Status -------------------------
2
1747
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 that match a pair of values for Field_A and Field_B should execute blindingly fast (in the index count the number of leaf nodes for the B-Tree node representing the indexed pair of values). My query looks like: SELECT count(*) FROM `table_A`...
7
4365
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 huge and I'm getting a time out. I thought I could do this with a stored procedure using the TableName as a
1
3047
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 = GridView1.Rows.Count.ToString(); allquestions.Select().Count.ToString(); is reporting the correct value, but GridView1.Rows.Count.ToString(); is reporting the incorrect value (it is reporting 0)
0
843
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
1632
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 above 2 tables, following this query i need to count all the records depending on a certaion value... at the moment ive got them counting from just one table which is fine. rs6=cn.execute ("Select count(*) as cardiffdeparts from...
7
6384
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 and 6 rows. Something like that. one two three four five one two three four five one two three four five one two three four five one two three four five
1
1266
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 "connect.php";
0
8162
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8415
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5532
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4045
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.