Re: return # of rows
| From: | Steven Campbell | Date: | Mon, 03 Jul 2000 14:00:32 +0000 |
| Subject: | Re: return # of rows | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-825@lists.php.net to get a copy of this message | ||
Manuel Lemos wrote:
>
> Hello Steven,
>
> >Why not do a $query = "SELECT COUNT(ROWNAME) FROM TABLENAME";
>
> >?
>
> Sure, that's the recommended way if can be sure that there is no chance
> that that the count of rows will not change between the two selects. Keep
> in mind that databases may be accessed by multiple users that may cause it
> to be changed and the query results may noy be consistent if you don't take the
> necessary precautions.
>
> On a side now, don't forget that count(rowname) may return a different
> number than count(*) if row name has NULLs on it.
>
> >True, PHP doesn't handle the count, MS-SQL does. But it's guaranteed to
> >be accurate (from whenever you executed the query plus a few
> >milliseconds of lag time).
>
> If nobody else changes the database as noted above.
>
> >Then you don't need to worry about broken odbc_num_rows anymore...
>
> Notice that odbc_num_rows is not broken. It just returns what the ODBC
> driver returns.
Note that under Microsoft SQL, the COUNT aggregate function with a (*)
argument returns the count of _ALL_ rows, not just NON-NULL values...
The following should do as you ask, as long as you have M$ SQL. It will
probably also work under variants of Sybase:
BEGIN
-- do a heavy duty lock on all tables involved in queries below, until
the locks are released
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT COUNT(*) FROM TABLENAME WITH (HOLDLOCK)
-- set locking back to default
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
END