简体   繁体   中英

PDO equivalent of mysql_num_rows

I want to be able to translate the mysql code below into PDO equivalent. Please can someone help me out as I have looked around and tried other examples but they are not working for me.

// Count Participants
$result = mysql_query("SELECT * FROM table2");
$num_rows = mysql_num_rows($result);

You can either just issue a count query to the DB

$db    = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$count = $db->query('SELECT count(*) FROM table2')->fetchColumn();

or get an array back that can be counted

$stmt  = $db->query('SELECT * FROM table2');
$rows  = $stmt->fetchAll(PDO::FETCH_ASSOC);
$count = count( $rows );

or get any kind of other resultset back, and count that etc.

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