我对php很新,我想在mysql列上做两个选择Counts并将它们放入一个数组中。我的代码如下,但这不起作用。
// 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);
嗨谢谢您的快速回复我也收到以下错误消息 PHP解析错误:语法错误,以下行的意外T_VARIABLE。
// Execute query
$result = mysqli_query($link,$sql);
答案 0 :(得分:0)
更改
$result_array[] = $array[];
到
$result_array = $array;
您也可以通过使用以下查询来提升性能。
SELECT Sum(Case When Status = 0 Then 1 Else 0),
Sum(Case When Status = 3 Then 1 Else 0)
FROM CE_TRANSACTIONS
答案 1 :(得分:0)
我相信您的问题来自$array[];
[]说的是'访问'的值,但您没有访问该阵列的任何索引。
您想要做的是:
$result_array[] = $array
答案 2 :(得分:0)
PHP Parse error: syntax error, unexpected T_VARIABLE
是因为你的行
$sql = "SELECT (SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 0),
(SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 3);"
$sql = "SELECT (SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 0),
(SELECT COUNT(STATUS) FROM CE_TRANSACTIONS WHERE STATUS = 3);"
缺少尾随。