Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Number of Rows from a Select statement

Tags:

sql

php

count

pdo

odbc

I have this:

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $username, $password);

$sql = "SELECT * FROM this_table";

$stmt = $dbh->query($sql);

//num of rows?

How do I get the number of rows returned from that SELECT statement?

Thanks all

like image 693
Abs Avatar asked Feb 21 '10 00:02

Abs


People also ask

How do I find the number of rows in a resultset in SQL?

To number rows in a result set, you have to use an SQL window function called ROW_NUMBER() . This function assigns a sequential integer number to each result row. However, it can also be used to number records in different ways, such as by subsets.

Which function is used to get the number of rows returned from a SELECT query?

The SQL COUNT function is used to count the number of rows returned in a SELECT statement.

How many rows are returned by the SQL statement?

Rationale: The union all subquery (aliased TA ) returns 2 rows, each containing the same value, that is the count of records in T .


1 Answers

SELECT count(*) FROM this_table is an option...

Regarding rowCount:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. **

However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

like image 161
Leniel Maccaferri Avatar answered Sep 27 '22 19:09

Leniel Maccaferri