[英]Counting results returned within php, efficiently
Technologies in use: php accessing MySQL via PDO. 使用的技术:php通过PDO访问MySQL。
I have the following query: 我有以下查询:
try {
$stmt = $conn->prepare("SELECT * FROM $database.appsLodged WHERE `uID` = :uid");
$stmt->bindValue(':uid', $uid);
$stmt->execute();
$result = $stmt->fetchAll();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
Which is fairly straight forward. 这是相当简单的。 At the moment there are 7 rows in the relevant database, and each one can potentially have a different string value returned for $result[0][5].
目前,相关数据库中有7行,每行可能为$ result [0] [5]返回不同的字符串值。 I would like to be able to count how many results there are that have a $result[0][5] of "shoes and socks" and then how many have a $result[0][5] of "boots and skirts" and how many have a $result[0][5] of "jumpers and cardigans" - without having to resort to doing multiple database queries (I know we can do it by adding an additional "AND
item
= :item" syntax and binding :item to "shoes and socks", but this seems very repetitive and unnecessary. 我希望能够计算出有$ result [0] [5]为“鞋子和袜子”的结果有多少,然后有$ result [0] [5]为“靴子和裙子”的结果为多少以及有多少个具有$结果[0] [5]的“跳线和开襟衫”-无需诉诸多个数据库查询(我知道我们可以通过添加其他“ AND
item
=:item”语法和绑定来做到这一点:item为“鞋子和袜子”,但这似乎非常重复且不必要。
Suggestions please, all welcome! 请提出建议,一切欢迎! :)
:)
C C
As requested below, here is the $result array listed: 按照下面的要求,这是列出的$ result数组:
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Pending
[5] => Pending
[dateStarted] => 1421773018
[6] => 1421773018
[lastModified] => 1421773018
[7] => 1421773018
)
[1] => Array
(
[id] => 2
[0] => 2
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1421774389
[6] => 1421774389
[lastModified] => 1421774389
[7] => 1421774389
)
[2] => Array
(
[id] => 3
[0] => 3
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1421776146
[6] => 1421776146
[lastModified] => 1421776146
[7] => 1421776146
)
[3] => Array
(
[id] => 4
[0] => 4
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1421777460
[6] => 1421777460
[lastModified] => 1421777460
[7] => 1421777460
)
[4] => Array
(
[id] => 5
[0] => 5
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1421781756
[6] => 1421781756
[lastModified] => 1421781756
[7] => 1421781756
)
[5] => Array
(
[id] => 6
[0] => 6
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422213946
[6] => 1422213946
[lastModified] => 1422213946
[7] => 1422213946
)
[6] => Array
(
[id] => 7
[0] => 7
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422238026
[6] => 1422238026
[lastModified] => 1422238026
[7] => 1422238026
)
[7] => Array
(
[id] => 8
[0] => 8
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422369458
[6] => 1422369458
[lastModified] => 1422369458
[7] => 1422369458
)
[8] => Array
(
[id] => 9
[0] => 9
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422369473
[6] => 1422369473
[lastModified] => 1422369473
[7] => 1422369473
)
[9] => Array
(
[id] => 10
[0] => 10
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422371233
[6] => 1422371233
[lastModified] => 1422371233
[7] => 1422371233
)
[10] => Array
(
[id] => 11
[0] => 11
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422371291
[6] => 1422371291
[lastModified] => 1422371291
[7] => 1422371291
)
[11] => Array
(
[id] => 12
[0] => 12
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422372793
[6] => 1422372793
[lastModified] => 1422372793
[7] => 1422372793
)
[12] => Array
(
[id] => 13
[0] => 13
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422373414
[6] => 1422373414
[lastModified] => 1422373414
[7] => 1422373414
)
[13] => Array
(
[id] => 14
[0] => 14
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422373681
[6] => 1422373681
[lastModified] => 1422373681
[7] => 1422373681
)
[14] => Array
(
[id] => 15
[0] => 15
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422373927
[6] => 1422373927
[lastModified] => 1422373927
[7] => 1422373927
)
[15] => Array
(
[id] => 16
[0] => 16
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422374004
[6] => 1422374004
[lastModified] => 1422374004
[7] => 1422374004
)
[16] => Array
(
[id] => 17
[0] => 17
[appID] => 4
[1] => 4
[applicationKey] => ConSupAp
[2] => ConSupAp
[applicationName] => Conference Support Application
[3] => Conference Support Application
[uID] => 1
[4] => 1
[status] => Data Acquisition
[5] => Data Acquisition
[dateStarted] => 1422374587
[6] => 1422374587
[lastModified] => 1422374587
[7] => 1422374587
)
)
Important to remember - it is actually the 'status' column I am wanting to count which for all of these examples except the first, is set to "Data Acquisition". 重要的是要记住-实际上,我想对“状态”列进行计数,除第一个示例外,所有这些示例的“状态”列均设置为“数据采集”。 The first is set to "Pending".
第一个设置为“待处理”。 There are another three values this could be, and if these are not in the database at the time I need a zero count for them so the user knows they have zero data set to other potential 'status' settings.
可能还有另外三个值,如果当时这些值不在数据库中,则我需要为它们提供零计数,以便用户知道将零数据设置为其他可能的“状态”设置。
I suggest you do the following: first of all, you take from you database all those strings, so you will have something like $result[0][5]
, $result[1][5]
, $result[2][5]
and so on, then you foreach()
them and match the strings in different variables, one for each string you want to match... I think, based on what you said in you query, that each uID has one type of string in the [5] position. 我建议您执行以下操作:首先,从数据库中获取所有这些字符串,因此您将拥有类似
$result[0][5]
, $result[1][5]
, $result[2][5]
,依此类推,然后将它们foreach()
匹配,并在不同的变量中匹配字符串,对于每个要匹配的字符串,一个...我想根据您在查询中所说的,每个uID都有一种字符串位于[5]位置。 In witch case, I advice you have a string of id's, so your query becomes something like this: 在女巫的情况下,我建议您有一个id的字符串,因此您的查询将如下所示:
$uid = '1,2,3,4,5'; // for example
$stmt = $conn->prepare("SELECT * FROM $database.appsLodged WHERE `uID` IN (:uid)");
$stmt->bindValue(':uid', $uid);
you will probably need one extra query to take all those ids but that's about it :D 您可能需要一个额外的查询来获取所有这些ID,仅此而已:D
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.