echo不能用來輸出陣列
阿新 • • 發佈:2018-11-17
輸出陣列的種方式:
function xiu_fetch_all($sql) {
$conn = mysqli_connect(XIU_DB_HOST, XIU_DB_USER, XIU_DB_PASS, XIU_DB_NAME);
if (!$conn) {
exit('連線資料庫失敗');
}
$query = mysqli_query($conn, $sql);
if (!$query) {
//查詢失敗
return false;
}
while ($row = mysqli_fetch_assoc($query)) {
$result[] = $row;
}
mysqli_free_result($query);
mysqli_close($conn);
return $result;
}
$all = xiu_fetch_all("select count(1) as num from posts ;");
//var_dump();
var_dump($all);
//json_encode();
$new_all = json_encod($all);
//結果:array(1) {
[0]=>array(1) {
["num"]=>string(1) "4"
}
}
echo $new_all; //[{"num":"4"}]
//print_r();輸出陣列
print_r($all);
//Array ( [0] => Array ( [num] => 4 ) )