2
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);
}

The echo json_encode($data); print nothing but print_r($data) print an array. How to convert the data array to json format.

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
        ) 
)
8
  • try using yii2 encode() function: yiiframework.com/doc-2.0/… Commented Aug 5, 2015 at 6:05
  • can you show us what is there in print_r($data) ? Commented Aug 5, 2015 at 6:06
  • What exactly does var_dump(json_encode($data)); return Commented Aug 5, 2015 at 6:07
  • 1
    var_dump(json_encode($data)); return bool(false) Commented Aug 5, 2015 at 6:26
  • 1
    If that is the case you should then call json_last_error_msg() to see what's gone wrong Commented Aug 5, 2015 at 6:54

1 Answer 1

3

Well I found the solution.

Havelock suggested to print json_last_error_msg() and I found malformed UTF-8 characters, possibly incorrectly encoded.

I am using MySQLi so I have to put this line in after database connection for character formatting: mysqli_set_charset($conn, "utf8");.

Thanks Havelock.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.