Json_encode failing on special chars and array_map throws 500 error

13 Views Asked by At

I've got a simple bit of php that is reading data from a SQL db and json encoding the rows and echoing it out.

It works a treat until any of the columns contain any special chars; then the output is empty.

I understand that JSON_ENCODE needs the data in UTF-8, and so tried to use array_map encode the data correctly, however this results in an immediate 500 error.

if ( $stmt ){
    if(sqlsrv_has_rows( $stmt )){
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
            $row = array_map('utf8_encode', $row);
            echo json_encode($row);
        }
    }
}

I have no idea why array_map isn't happy.

Can anyone point me in the right direction?

0

There are 0 best solutions below