So sánh count và num_row php

(PHP 4, PHP 5)

Show

mysql_num_rows — Get number of rows in result

Description

mysql_num_rows(resource $result): int|false

Parameters

result

The result resource that is being evaluated. This result comes from a call tomysql_query().

Return Values

The number of rows in a result set on success or false on failure.

Examples

Example

1 mysql_num_rows() example

`

$link

= mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("database", $link);$result = mysql_query("SELECT * FROM table1", $link); $num_rows = mysql_num_rows($result);

echo

"$num_rows Rows\n";?>`

Notes

Note: If you use mysql_unbuffered_query(), mysql_num_rows() will not return the correct value until all the rows in the result set have been retrieved.
Note: For backward compatibility, the following deprecated alias may be used: mysql_numrows()

See Also

  • mysql_affected_rows() - Get number of affected rows in previous MySQL operation
  • mysql_connect() - Open a connection to a MySQL Server
  • mysql_data_seek() - Move internal result pointer
  • mysql_select_db() - Select a MySQL database
  • mysql_query() - Send a MySQL query

19 years ago

`Some user comments on this page, and some resources including the FAQ at :

suggest using count(*) to count the number of rows

This is not a particularly universal solution, and those who read these comments on this page should also be aware that

select count() may not give correct results if you are using "group by" or "having" in your query, as count() is an agregate function and resets eachtime a group-by column changes.

select sum(..) ... left join .. group by ... having ...

can be an alternative to sub-selects in mysql 3, and such queries cannot have the select fields replaced by count(*) to give good results, it just doesn't work.

Sam

`

19 years ago

`Re my last entry:

This seems the best workaround to get an 'ordinary' loop going, with possibility of altering output according to row number (eg laying out a schedule)

$rowno=mysql_num_rows($result);

for ($i=0; $i

print "

"; print "".$row['timeon']."-".$row['timeoff']." ".$row['event']."
; if ($i!=$rowno-1) { print "other-html-within-sched-here
"; } else print "end-last-entry-html-here
"; } //close loop

`

20 years ago

`I may indeed be the only one ever to encounter this - however if you have a myisam table with one row, and you search with valid table and column name for a result where you might expect 0 rows, you will not get 0, you will get 1, which is the myisam optimised response when a table has 0 or one rows. Under "5.2.4 How MySQL Optimises WHERE Clauses" it reads:

*Early detection of invalid constant expressions. MySQL quickly detects that some SELECT statements are impossible and returns no rows.

and

*All constant tables are read first, before any other tables in the query. A constant table is:

  1. An empty table or a table with 1 row.
  2. A table that is used with a WHERE clause on a UNIQUE index, or a PRIMARY KEY, where all index parts are used with constant expressions and the index parts are defined as NOT NULL.

Hopefully this will keep someone from staying up all night with 1146 errors, unless I am completely mistaken in thinking I have this figured out.

`

8 years ago

`In Reply to the last post: This may not always work correctly, as $object->doesExist would contain a result, not a boolean value. A better way (using the same method) would be using a cast:

[...] function

load() { $result = mysql_query('...'); $this->doesExist = (bool) ($res = mysql_fetch_array($result)) [...] } } ?>

`

18 years ago

`In response to oran at trifeed dot com:

You are only experiencing this behaviour because you have not given your FOUND_ROWS() result an alias:

$qry = mysql_query ( 'SELECT FOUND_ROWS() AS total' ); $rst = mysql_fetch_array ( $qry, MYSQL_ASSOC ); echo $rst['total'];

Sean :)

`

16 years ago

A note on the following usage; that suggest to use several MySQL Functions to get the number of Table Records.

You may be familiar with following:

$sqlQuery

= 'Select SQL_CALC_FOUND_ROWS MyField From MyTable Limit 1;'; $sqlQuery_1 = 'Select FOUND_ROWS( );';?>

I omitted the actual connection to MySQL and the execution of the query, but you get the idea.

I did some tests and on a fairly high traffic web site, one that executes several queries quite often and found that using this combination of MySQL Functions can actually result in wrong results.

For example, assume I have two queries to get the number of Table Records in two different Tables. So in essence, we are executing 4 queries ( 2 queries for each Table ).

If two different requests come in through PHP, your going to run into problems. Note than when I mean request, I mean two different clients requesting your PHP page.


Request 1:


Execute: SQL_CALC_FOUND_ROWS On Table 1


Request 2:


Execute: SQL_CALC_FOUND_ROWS On Table 2


Request 1:


Execute: Select FOUND_ROWS( )

At this point, you see the race condition that occurred. While Request 1 was being executed, Request 2 came in.

At this point Request 1 will return the number of Table Records in Table 2 and not Table 1 as expected!

Why? Because MySQL does not differ between requests. Each query is in a queue waiting its turn. As soon as its turn comes in it will be executed my MySQL.

The MySQL Function Select FOUND_ROWS( ) will return the result of the last SQL_CALC_FOUND_ROWS!

Keep in mind.

18 years ago

`result`0

18 years ago

`result`1

21 years ago

`result`2

18 years ago

`result`3

19 years ago

`result`4

15 years ago

`result`5

13 years ago

`result`6

12 years ago

`result`7

16 years ago

`result`8

18 years ago

`result`9

15 years ago

`false`0

15 years ago

`false`1

16 years ago

`false`2

15 years ago

`false`3

18 years ago

`false`4