简体   繁体   English

PDO相当于mysql_num_rows

[英]PDO equivalent of mysql_num_rows

I want to be able to translate the mysql code below into PDO equivalent. 我希望能够将下面的mysql代码转换为等效的PDO。 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 您可以只向数据库发出count查询

$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. 或取回任何其他结果集,并进行计算等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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