gpt4 book ai didi

php - PDO 联盟计数

转载 作者:可可西里 更新时间:2023-11-01 08:10:58 27 4
gpt4 key购买 nike

public function totalRecords($keyword) {
$sql = "SELECT COUNT(uid) FROM user_information WHERE title LIKE ? OR name LIKE ? OR surname LIKE ?
UNION ALL
SELECT COUNT(id) FROM groups WHERE name LIKE ?";
$query = $this->db->prepare($sql);
$query->execute(array("%$keyword%", "%$keyword%", "%$keyword%", "%$keyword%"));
return $query->rowCount();
}

我正在尝试创建寻呼机,我需要对搜索词条进行总记录。

上面的代码不起作用,它总是返回 0。我该怎么做?

最佳答案

rowCount()

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

所以你必须使用 fetch()sum()

public function totalRecords($keyword) {
$sql = "SELECT SUM(num) as num FROM
(SELECT COUNT(uid) as num FROM user_information WHERE title LIKE ? OR name LIKE ? OR surname LIKE ?
UNION ALL
SELECT COUNT(id)as num FROM groups WHERE name LIKE ?)
AS X";
$query = $this->db->prepare($sql);
$query->execute(array("%$keyword%", "%$keyword%", "%$keyword%", "%$keyword%"));
$Total = $query->fetch();
return $Total['num'];
}

关于php - PDO 联盟计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34015120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com