0

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?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
John
  • 755
  • 1
  • 18
  • 46
  • 1
    Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Oct 17 '22 at 12:42
  • 1
    Can you please check the error log? You will find the error description in the error log. So you can understand why array_map is not happy. – Hkachhia Oct 17 '22 at 12:42
  • 1
    Read the Duplicate Q&A That will almost definitely describe your problem and a number of solutions – RiggsFolly Oct 17 '22 at 12:49

0 Answers0