473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($sq l);
$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($sq l);
$result = mysql_fetch_ass oc( $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 9640
On Jul 26, 2:14 am, FFMG <FFMG.2ub...@ no-mx.httppoint.co mwrote:
Hi,

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

$sql = "SELECT * FROM TABLE";
$result = mysql_query($sq l);
$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($sq l);
$result = mysql_fetch_ass oc( $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.c om, (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.co m
wrote:
>
Hi,

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

$sql = "SELECT * FROM TABLE";
$result = mysql_query($sq l);
$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($sq l);
$result = mysql_fetch_ass oc( $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.comwro te:
| 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...@y ahoo.comwrote:
On 26 Jul, 14:20, Rik <luiheidsgoe... @hotmail.comwro te:
| 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...@gmai l.comwrote:
On Jul 26, 3:03 pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:


On 26 Jul, 14:20, Rik <luiheidsgoe... @hotmail.comwro te:
| 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
6250
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 web page to the 'file' field in the table - I don't understand why it is doing that - I mean, isn't the SELECT DISTINCT statement only pulling those records from that one web page? I guess I just don't follow what it is doing with that SELECT...
7
1831
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 select from my database the records that I need, but I need to insert them somewhere down the page so that I can process them. Here's my select statement......
2
5262
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 i am making i am asking users to select from list (e.g. nationality) however i would like to avoid typing out every possible value of which there are about 40 (which change). I would much prefer to keep a seperate table in my database for the...
2
1633
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 within it. Pages contain a 'sectionid' field to join it to the 'id' in the seciton table but im unsure how to join, and group to get a count of the number of pages in each section (i.e. in each result row)
2
1601
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 and then did another query on each record while looping through those results to get what I am trying to do in one query now. I would simply do that same thing but I am not sure how while using Visual Studio and I know that it was bad code, so...
6
2289
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 produce what i would like to be held in the select (dropdown) list. i then create the select list with a delete button. I then try to create the query to delete the selected option from the list from the database, but nothing happens and an error...
0
2031
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 = mysql_use_result(my_db); num_row = mysql_num_rows(res); count is returned as 0 (no error) er is NULL (no error)
6
2026
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 (mysql_error()); mysql_select_db("Shop_Test",$conn) or die (mysql_error()); $display_block = "<h1> My Store </h1>"; $get_item = "SELECT c.id as Cat_ID, c.cat_title, SI.Item_title,
1
1324
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 ("localhost","root","") or die (mysql_error ()); mysql_select_db ("stest",$conn) or die (mysql_error());
0
7880
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8255
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...
1
8010
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
6665
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...
1
5739
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
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
3868
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
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.