24

Right now I have a PHP file that does a MYSQL query and then counts rows like this:

$count=mysql_num_rows($result);


if ($count == 1) {
    $message = array('status' => 'ok');
} else {
    $message = array('status' => 'error');
}

This works fine but I'm trying to change all my PHP files to use PDO. So how can this be done with PDO?

2

5 Answers 5

24
$res = $DB->query('SELECT COUNT(*) FROM table');
$num_rows = $res->fetchColumn();

or

$res = $DB->prepare('SELECT COUNT(*) FROM table');
$res->execute();
$num_rows = $res->fetchColumn();

You can use this to ask if data exists or is selected, too:

$res = $DB->query('SELECT COUNT(*) FROM table');
$data_exists = ($res->fetchColumn() > 0) ? true : false;

Or with your variables:

$res = $DB->query('SELECT COUNT(*) FROM table');
$message = ($res->fetchColumn() > 0) ? array('status' => 'ok') : array('status' => 'error');
Sign up to request clarification or add additional context in comments.

3 Comments

am i wrong or You will get only "first column of the next row" uk3.php.net/manual/en/pdostatement.fetchcolumn.php
Without a parameter in fetchColumn you will get the first column of the selected row. But we only get 1 result and that's the count. The next row means, when you didn't have fetched a row or column, you will begin with the first row of results. After you call fetchColumn again, you will get the content of the next row.
Yes, fetchcolumn() returns the first colmn of the row, rather, you need to use rowCount()
20
$stmt = $db->query('SELECT * FROM table');  
$row_count = $stmt->rowCount();  
echo $row_count.' rows selected';

1 Comment

php.net/manual/en/pdostatement.rowcount.php says: 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 behavior is not guaranteed for all databases and should not be relied on for portable applications.
6

Maybe you can use PDO's "fetchAll" method, which returns an array containing all the SELECT results. Then use "count" method to count the array's rows.

Ex:

$rows = $stmt->fetchAll();
$num_rows = count($rows);

4 Comments

This could be expensive on a large dataset if all you need to know is the number of results.
@QuinnComendant, the rowCount() function returns an accurate count only after you fetch all the rows anyway.
@BillKarwin, did you mean mysql_num_rows()? PDO's rowCount() method "returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement"—not the same thing.
@QuinnComendant, check the code in ext/pdo_mysql/mysql_statement.c, in function pdo_mysql_fill_stmt_from_result(). In the MySQL driver at least, PDOStatement::rowCount() works for both affected rows and result-set rows.
3

If you are not using prepared statements then try:

$find = $dbh->query('SELECT count(*) from table');
if ($find->fetchColumn() > 0){
    echo 'found';
}

However, if you choose prepared statements, which i highly recommend, then:

$find = $dbh->prepare('SELECT count(*) from table');
$find->execute();
if ($find->fetchColumn() > 0){
    echo 'found';
}

Comments

1

Can be like that...

$numRows = $conn->query("SELECT COUNT(*) FROM yourtable")->fetchColumn();
echo $numRows; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.