i am quite new to php and I want to do two select Counts on a mysql column and put these into an array. The code i have is below however this does not work.
// DB connection
$link = mysqli_connect( "localhost", "username", "password", "CE_TRANSACTIONS" );
// Query users table
$sql = "SELECT (SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 0),
(SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 3);"
// Execute query
$result = mysqli_query($link,$sql);
// Loop over all result rows
$result_array = array();
while($array = mysqli_fetch_assoc($result))
{
$result_array[] = $array[];
}
// Write to JSON
echo json_encode($result_array);
Hi thanks for the quick replies i am also getting the following error message PHP Parse error: syntax error, unexpected T_VARIABLE for the below line.
// Execute query
$result = mysqli_query($link,$sql);
Change
$result_array[] = $array[];
to
$result_array = $array;
You may also see performance boost by using the following query instead.
SELECT Sum(Case When Status = 0 Then 1 Else 0),
Sum(Case When Status = 3 Then 1 Else 0)
FROM CE_TRANSACTIONS
I believe your problem is coming from $array[];
What the [] is saying is 'Access the value at', but you're not accessing any index of the array.
What you want to do is: $result_array[] = $array
The PHP Parse error: syntax error, unexpected T_VARIABLE
is because your line
$sql = "SELECT (SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 0), (SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 3);"
is missing a trailing ;
.
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.