存储架构

Search all values

微信扫一扫,分享到朋友圈

Search all values
0

I have this code:

$query_search_select = $db->prepare("SELECT * , COUNT(*) AS coun FROM users WHERE user LIKE :keyword ;");
$query_search_select->bindParam(":keyword" , $key_search , PDO::PARAM_STR ) ;
$query_search_select->execute();

The query works, but when i take the information as this:

while($res = $query_search_select->fetch(PDO::FETCH_ASSOC)){
// code
}

But the loop during only 1 time, in COUNT(*) result 2 (it is right) but this loop during only 1 time… why? I search on stackoverflow and internet, i’ve tried fetchAll but with it i’ve the same problem…

the problem here is, that COUNT()
is a GROUP BY
or aggregate function

(see http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
)

so if you are using COUNT()
in your MySQL
statement, your resultset is automatically grouped to one row.

but you still can do it like this:

"SELECT * , (SELECT COUNT(*) FROM users WHERE user LIKE :keyword) AS coun FROM users WHERE user LIKE :keyword2"

NOTE:you’ll have to add another parameter binding since parameters can only be used once in the MySQL
statement:

$query_search_select->bindParam(":keyword2" , $key_search , PDO::PARAM_STR ) ;

阅读原文...


微信扫一扫,分享到朋友圈

Search all values
0

Hello, buddy!

经纪时代,中间人是如何创造价值的

上一篇

JMeter Variable output in the property name?

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Search all values

长按储存图像,分享给朋友