-2

I am using this PDO Query to select from a database:

$stmt = $pdo_conn->prepare("select * from tickets where ticketnumber = :seq ");
$stmt->execute(array(':seq' => $_GET["seq"]));
$ticket = $stmt->fetch();

but i cannot get an equivalent to mysql_num_rows

I have tried doing:

$stmt = $pdo_conn->prepare("select COUNT(*), * from tickets where ticketnumber = :seq ");
$stmt->execute(array(':seq' => $_GET["seq"]));
$ticket = $stmt->fetch();
$num_rows = $stmt->fetchColumn();
echo $num_rows;
charlie
  • 1,356
  • 7
  • 38
  • 76

2 Answers2

3

Did you bother looking at the manual?

http://us1.php.net/mysql_num_rows

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_stmt_num_rows() PDOStatement::rowCount()

http://us1.php.net/manual/en/pdostatement.rowcount.php

See: Example #2 Counting rows returned by a SELECT statement

With your code it would be:

select COUNT(*) from tickets where ticketnumber = :seq 

You can't select a count and * at the same time. The example shows you to select the count, THEN try to select your rows.

Jessica
  • 7,075
  • 28
  • 39
1

Firstly, you'll need to do 2 separate querys to get the row count as you can't do a COUNT() and * in the same query.

So you would get the row count using:

$stmt = $pdo_conn->prepare("select * from tickets where ticketnumber = :seq ");
$stmt->execute(array(':seq' => $_GET["seq"]));
$ticket = $stmt->fetch();

echo 'Rows: '.$stmt -> rowCount();

More information can be found in the manual: PDOStatement::rowCount

ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • 1
    Why the down vote? It's the correct answer. – ajtrichards Jan 02 '14 at 18:28
  • This does work when the database PDO is connecting to is MySQL. It will give you the actual number of rows in the result. Although the PHP manual says it won't work for most databases other than MySQL. And even in MySQL, it does *not* function like the MySQL `FOUND_ROWS()` function, which allows you to find out how many rows you would have gotten if you hadn't used a `LIMIT` clause. http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows – Buttle Butkus Apr 27 '16 at 03:43