使用关联数组进行PHP到JSON编码
使用关联数组进行PHP到JSON编码,php,json,associative-array,Php,Json,Associative Array,echo json_encode($data)只打印($data)打印数组,不打印任何内容。如何将数据数组转换为json格式 require "constant.php"; require "database.php"; $sql = "select e.*, b.booth_address, ba.bank_full_name from $app_booth_enrolment e left join $app_booth_info b on e.booth_id=b.booth_id lef
echo json_encode($data)代码>只打印($data)
打印数组,不打印任何内容。如何将数据数组转换为json格式
require "constant.php";
require "database.php";
$sql = "select e.*, b.booth_address, ba.bank_full_name from $app_booth_enrolment e
left join $app_booth_info b on e.booth_id=b.booth_id
left join $app_bank_info ba on e.bank_id=ba.bank_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
$data = array();
while($row = $result->fetch_assoc()){
array_push($data,$row);
}
echo $data = json_encode($data);
var_dump(json_encode($data));
}else{
$data['status'] = "No terminal found";
echo $data = json_encode($data);
}
我找到了解决办法 Havelock建议打印
json\u last\u error\u msg()
,我发现格式错误的UTF-8字符,可能编码不正确
我使用的是MySQLi
,所以我必须在数据库连接后插入这一行以进行字符格式设置:MySQLi\u set\u字符集($conn,“utf8”)代码>
谢谢Havelock。尝试使用yii2 encode()函数:您能告诉我们打印($data)
中有什么吗?var_转储(json_encode($data))的确切功能是什么代码>returnvar_dump(json_encode($data));返回bool(false),如果是这种情况,那么您应该打电话查看出了什么问题
Array
(
[0] => Array
(
[id] => 1
[enrolment_id] => 101-1-0001
[terminal_type_id] => ATM
[booth_name] => CITY-Bashundhara
[enrolment_date] => 2015-01-01
[live_date] => 2015-02-02
[bank_id] => 101
[booth_id] => 101-0001
[terminal_serial] => 5300153835
[terminal_host] => TCBL-TWO
[terminal_app] =>
[note] =>
[status] => 1
[insert_by] => 1
[insert_date] =>
[booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000
[bank_full_name] => The City Bank Limited
)
[1] => Array
(
[id] => 2
[enrolment_id] => 101-1-0002
[terminal_type_id] => ATM
[booth_name] => CITY-VIP_Road
[enrolment_date] => 2015-01-01
[live_date] => 2015-02-02
[bank_id] => 101
[booth_id] => 101-0002
[terminal_serial] => 5300153831
[terminal_host] => TCBL-TWO
[terminal_app] =>
[note] =>
[status] => 1
[insert_by] => 1
[insert_date] =>
[booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000
[bank_full_name] => The City Bank Limited
)
[2] => Array
(
[id] => 3
[enrolment_id] => 101-2-0003
[terminal_type_id] => CDM
[booth_name] => CITY-DSE
[enrolment_date] => 2015-01-01
[live_date] => 2015-02-02
[bank_id] => 101
[booth_id] => 101-0003
[terminal_serial] => 5300153832
[terminal_host] => TCBL-TWO
[terminal_app] =>
[note] =>
[status] => 1
[insert_by] => 1
[insert_date] =>
[booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000
[bank_full_name] => The City Bank Limited
)
)