简体   繁体   English

在一个数组中插入多个查询的结果

[英]Insert results of multiple queries in one array

This is how my PHP file looks right now: 这是我的PHP文件现在的外观:

<?php 
    ini_set('display_errors','1'); 
    error_reporting(E_ALL);
    include_once 'dbConnect.php';

    if (connect()){
        global $conn;
        $param = array();

        $queryOBParam = "SELECT p,q,b
                         FROM params
                         WHERE faculty = 'OB'";             

        $checkRes = $conn->prepare($queryOBParam);
        $checkRes->execute();
        $checkRes->bind_result($pOB,$qOB,$bOB);

        while ($checkRes->fetch()){ 
            $temp = [
                'p'=>$pOB,
                'q'=>$qOB,
                'b'=>$bOB
            ];

            array_push($param,$temp);
        }

        echo json_encode($param);
    }
?>

I wish to insert the results of the query below in the array $param since I need all of them in my application at one go. 我希望将下面的查询结果插入数组$ param中,因为我需要一次性将所有查询结果放入应用程序中。

$queryTotVoters = "SELECT COUNT(*) as totalVoters
                   FROM regVoter";

How do I proceed? 我该如何进行?

In a comment you said that your desired end array should look like this: [{"p":293,"q":433,"b":10, "totalVoters":27}] . 在评论中,您说您想要的最终数组应如下所示: [{"p":293,"q":433,"b":10, "totalVoters":27}]
Here is a way you can accomplish that, but keep in mind that however many entries the array will have, the totalVoters value will be the same for all, because the query SELECT COUNT(*) as totalVoters FROM regVoter does not have a WHERE clause like WHERE param = X . 这是您可以完成此操作的一种方法,但是请记住,无论数组将包含多少条目,totalVoters值对于所有对象都是相同的,因为SELECT COUNT(*) as totalVoters FROM regVoter的查询SELECT COUNT(*) as totalVoters FROM regVoter没有WHERE子句就像WHERE param = X

<?php 
    ini_set('display_errors','1'); 
    error_reporting(E_ALL);
    include_once 'dbConnect.php';

    if (connect()){
        global $conn;
        $param = array();
        $totalVoters = 0;

        // get totalVoters from regVoter
        $queryTotalVoters = "SELECT COUNT(*) as totalVoters FROM regVoter";

        $checkRes = $conn->prepare($queryTotalVoters);
        $checkRes->execute();
        $checkRes->bind_result($resTotalVoters);

        while ($checkRes->fetch()){ 
            $totalVoters = $resTotalVoters;
        }


        $queryOBParam = "SELECT p,q,b
                         FROM params
                         WHERE faculty = 'OB'";             

        $checkRes = $conn->prepare($queryOBParam);
        $checkRes->execute();
        $checkRes->bind_result($pOB,$qOB,$bOB);

        while ($checkRes->fetch()){ 
            $param[] = [
                'p'=>$pOB,
                'q'=>$qOB,
                'b'=>$bOB,
                'totalVoters'=>$totalVoters
            ];
        }

        echo json_encode($param);
    }
?>

small note: I have never used bind_result() and fetch(), in fact objectorientated Mysqli in general, so I just copied it from the existing query. 小提示:我从未使用过bind_result()和fetch(),实际上通常是面向对象的Mysqli,所以我只是从现有查询中复制了它。 Therefore my code may have errors 因此我的代码可能有错误

Edit: added initialisation of $totalVoters 编辑:添加了$ totalVoters的初始化

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

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