简体   繁体   中英

How to get mysql_num_rows with PDO?

How can I easily get the number of rows returned with " SELECT * " SQL-query?

function valid_credentials($db,$name,$pw) {
    try {

    $sth = $db->prepare("SELECT * FROM ib_members WHERE name=:val AND pw=:val2");
    $sth->bindValue(":val",$name);
    $sth->bindValue(":val2",$pw);   
    $sth->execute();


    $numrows = $sth->fetchColumn();

    return $numrows;

    } catch (PDOException $e) {

        return $e->getMessage();

    }
}

That returns 14, which is definately not the number of rows returned, but it's the ID that the first row has.

For DELETE , INSERT , or UPDATE statements, use PDOStatement::rowCount() .

For SELECT statements, count the rows manually in your while( $sth->fetch() ) loop (you can break out of the loop if the count exceeds a certain threshold, for instance), or execute a separate query to have the database return the row count (for example, SELECT COUNT(*) FROM table WHERE column = ? ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM