简体   繁体   中英

Counting results returned within php, efficiently

Technologies in use: php accessing MySQL via PDO.

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]. 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.

Suggestions please, all welcome! :)

C

As requested below, here is the $result array listed:

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. In witch case, I advice you have a string of id's, so your query becomes something like this:

$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

Hope this helps!

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