473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

So, SELECT count(*)... or mysql_num_rows(...)


Hi,

I was running a test on a table with 50000 rows.
When I do:

$sql = "SELECT * FROM TABLE";
$result = mysql_query($sql);
$total = mysql_num_rows(result);

I get a 'run out of memory error', (the limit is set low on the test
server).

But when I do:

$sql = "SELECT count(*) as num FROM TABLE";
$result = mysql_query($sql);
$result = mysql_fetch_assoc( $result );
$total = $result['num'];

Everything works fine.

or is
$sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
...
even better?

So What is the 'preferred' method of getting a row count?

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
(http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php)
'Free URL redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=18782

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Jul 26 '07 #1
5 9627
On Jul 26, 2:14 am, FFMG <FFMG.2ub...@no-mx.httppoint.comwrote:
Hi,

I was running a test on a table with 50000 rows.
When I do:

$sql = "SELECT * FROM TABLE";
$result = mysql_query($sql);
$total = mysql_num_rows(result);

I get a 'run out of memory error', (the limit is set low on the test
server).

But when I do:

$sql = "SELECT count(*) as num FROM TABLE";
$result = mysql_query($sql);
$result = mysql_fetch_assoc( $result );
$total = $result['num'];

Everything works fine.

or is
$sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
..
even better?

So What is the 'preferred' method of getting a row count?

FFMG

--

'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
(http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php)
'Free URL redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile:http://www.httppoint.com/member.php?userid=580
View this thread:http://www.httppoint.com/showthread.php?t=18782

Message Posted via the webmaster forumhttp://www.httppoint.com, (Ad revenue sharing).
Always use "select count(*)" for this type of thing.

The database doesn't necessarily need to go and actually get all the
rows to figure it out this way. If you "select *" and then ask for
the row count he actually needs to go get all those rows, which is why
you're running out of memory.

Jul 26 '07 #2
Rik
On Thu, 26 Jul 2007 08:14:09 +0200, FFMG <FF*********@no-mx.httppoint.com
wrote:
>
Hi,

I was running a test on a table with 50000 rows.
When I do:

$sql = "SELECT * FROM TABLE";
$result = mysql_query($sql);
$total = mysql_num_rows(result);

I get a 'run out of memory error', (the limit is set low on the test
server).

But when I do:

$sql = "SELECT count(*) as num FROM TABLE";
$result = mysql_query($sql);
$result = mysql_fetch_assoc( $result );
$total = $result['num'];

Everything works fine.

or is
$sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
..
even better?

So What is the 'preferred' method of getting a row count?
If you don't need the rows themselves, keep use of resources at a low by
using a COUNT() construct. If you do need all the rows, use
mysql_num_rows(), underneath the surface it is essentially the same as
FOUND_ROWS(), with the added bonus of getting an integer straight back.
--
Rik Wasmus
Jul 26 '07 #3
On 26 Jul, 14:20, Rik <luiheidsgoe...@hotmail.comwrote:
| mysql_num_rows(), underneath the surface it is essentially the same
as
| FOUND_ROWS(), with the added bonus of getting an integer straight
back.

I don't think so Rik!

mysql_num_rows() will tell you how many rows have been returned in
this particular request.

FOUND_ROWS() will tell you how many rows would have been returned if
you did not have a LIMIT clause on the previous query and it also
requires the previous query to have had SQL_CALC_FOUND_ROWS in it.

SELECT FOUND_ROWS() AS num FROM TABLE

will return you the value of 0 for as many rows as there are in the
table.

Jul 26 '07 #4
On Jul 26, 3:03 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 26 Jul, 14:20, Rik <luiheidsgoe...@hotmail.comwrote:
| mysql_num_rows(), underneath the surface it is essentially the same
as
| FOUND_ROWS(), with the added bonus of getting an integer straight
back.

I don't think so Rik!

mysql_num_rows() will tell you how many rows have been returned in
this particular request.

FOUND_ROWS() will tell you how many rows would have been returned if
you did not have a LIMIT clause on the previous query and it also
requires the previous query to have had SQL_CALC_FOUND_ROWS in it.

SELECT FOUND_ROWS() AS num FROM TABLE

will return you the value of 0 for as many rows as there are in the
table.
For compatibility purposes use something standard like select count.

Jul 27 '07 #5
On 27 Jul, 14:24, Webrickco <webric...@gmail.comwrote:
On Jul 26, 3:03 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:


On 26 Jul, 14:20, Rik <luiheidsgoe...@hotmail.comwrote:
| mysql_num_rows(), underneath the surface it is essentially the same
as
| FOUND_ROWS(), with the added bonus of getting an integer straight
back.
I don't think so Rik!
mysql_num_rows() will tell you how many rows have been returned in
this particular request.
FOUND_ROWS() will tell you how many rows would have been returned if
you did not have a LIMIT clause on the previous query and it also
requires the previous query to have had SQL_CALC_FOUND_ROWS in it.
SELECT FOUND_ROWS() AS num FROM TABLE
will return you the value of 0 for as many rows as there are in the
table.
| For compatibility purposes use something standard like select count.

What has this got to do with my post?

Jul 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ralph Freshour | last post by:
I have a question about the following PHP script - I got it off a web site tutorial on how to count users logged into your site - my question is the $PHP_SELF variable - it writes the name of the...
7
by: Neil | last post by:
Hello, I do hope some kind soul can help me with what I thought was not going to be difficult, but for this newbie it's a bit harder than I thought. Here's what I'm trying to do..... I can...
2
by: phpuser32423 | last post by:
Hi everyone Is it by any chance possible to use mysql and php to auto create the content for drop-down lists on forms by retrieving the values from a database? The reason i ask is that on a site...
2
by: danny_m | last post by:
Hi there I have two tables, pages and sections, of which pages are a subset of sections. I'm listing out the sections in a list and want to include a COUNT of how many pages each section has...
2
Spazasaurus
by: Spazasaurus | last post by:
I am having trouble. I am not sure if it is not possible or not, but don't know any alternatives. I am converting my site from PHP and MYSQL to ASP.NET and MSSQL. In my current site. I did a query...
6
by: yc022 | last post by:
Hi all, this is my first time using this so i'm not really sure how it works. please bear with me. i am trying to delete a row in a database using a select list. First of all i have a query to...
0
by: richard | last post by:
I am having a problem with SELECT. The table has 3 rows. I am using the C api. Here is my C code. count = mysql_query(my_db, "SELECT * FROM accounts"); er = mysql_error(my_db); res =...
6
by: ashraf02 | last post by:
can some one please help me i am getting this error but i cant seem to figure out wat is wrong on line 3 everything looks fine <?php $conn = mysql_connect("localhost","root","") or die...
1
by: ashraf02 | last post by:
I am getting this error when i run the php file and cant figure out why. everything seems to look fine. the error i get is: here is my php code. <?php $conn = mysql_connect...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.