This is how my PHP file looks right now:
<?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.
$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}]
.
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
.
<?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. Therefore my code may have errors
Edit: added initialisation of $totalVoters
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.