Php rowcount is not working in pdo

So I am grabbing the amount of rows in a specific table where the username is already in the database like so:

$second_sql = $db->prepare["SELECT * FROM users WHERE username = :username"];    
$second_sql->bindParam[':username', $username];    
$second_sql->execute[];

if[$second_sql->rowCount[] == 1] {
  $db = null;
  header["Location: ../login/"];
} else {
  $statement->execute[];
  $db = null;
}

The problem is it's not working. If you need more of the script just tell me.

ArK

20.1k66 gold badges106 silver badges136 bronze badges

asked Aug 23, 2016 at 16:48

6

Some databases does not report the row count with PDO->rowCount[] method.

SQLite, for instance.

So don't use rowCount[]; doing so makes your code less portable.

Instead use the COUNT[*] function in your query, and store the result in a variable.

Finally, use that variable to fetch the one and only column [users] using the fetchColumn[] method.

So you can play with this:

try {
    $second_sql = $db->prepare["SELECT COUNT[*] from users WHERE username = :username"];
    $second_sql->bindParam[':username', $username, PDO::PARAM_STR];
    $second_sql->execute[];
    $count = $second_sql->fetchColumn[];
} catch [PDOException $e] {
    // Here you can log your error
    // or send an email
    // Never echo this exception on production
    // Only on development fase
    echo "Error: " . $e->getMessage[];
}

if [$count] {
    $db = null;
    header["Location: ../login/"];
} else {
    $statement->execute[];
    $db = null;
}

Perhaps you wanna test you condition for a single row:

if [$count == 1]

Hope this helps you.

Cheers!

answered Aug 23, 2016 at 23:10

Chris PesoaChris Pesoa

1662 silver badges3 bronze badges

Not the answer you're looking for? Browse other questions tagged php mysql pdo or ask your own question.

[PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0]

PDOStatement::rowCount Returns the number of rows affected by the last SQL statement

Description

public PDOStatement::rowCount[]: int

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.

Note:

This method returns "0" [zero] with the SQLite driver at all times, and with the PostgreSQL driver only when setting the PDO::ATTR_CURSOR statement attribute to PDO::CURSOR_SCROLL.

Parameters

This function has no parameters.

Return Values

Returns the number of rows.

Examples

Example #1 Return the number of deleted rows

PDOStatement::rowCount[] returns the number of rows affected by a DELETE, INSERT, or UPDATE statement.

The above example will output something similar to:

Return number of rows that were deleted:
Deleted 9 rows.

Example #2 Counting rows returned by a SELECT statement

For most databases, PDOStatement::rowCount[] does not return the number of rows affected by a SELECT statement. Instead, use PDO::query[] to issue a SELECT COUNT[*] statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn[] to retrieve the number of matching rows.

Chủ Đề