greatchap Posted July 10, 2020 Share Posted July 10, 2020 Hello Everyone, I have a PHP code that reads data from db table. Then it converts it into JSON and show it. However if data table field has - or so then it gives json error. For e.g. if($num > 0){ while($res = mysqli_fetch_array($query)){ $NDate = $res['NDate']; $Summary = substr($res['Summary'],0,256); /* CREATE ARRAY FOR SHORT TERM NEWSLETTER */ $newsletters[]=array('newsletter'=>array("RecID"=>$SNo,"Summary"=>$Summary)); } /* JSON OUTPUT*/ $json = array("AuthOK" => 'True',"newsletters"=>$newsletters); header('Content-type: application/json'); echo json_encode($json); How to solve the issue ? Regards, GR Quote Link to comment https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/ Share on other sites More sharing options...
requinix Posted July 10, 2020 Share Posted July 10, 2020 What is the JSON and what is the error? And are you using correct character encodings? UTF-8 for the database connection and tables and columns and strings and webpages and PHP settings and I could keep going? Quote Link to comment https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/#findComment-1579464 Share on other sites More sharing options...
greatchap Posted July 10, 2020 Author Share Posted July 10, 2020 2 hours ago, requinix said: What is the JSON and what is the error? And are you using correct character encodings? UTF-8 for the database connection and tables and columns and strings and webpages and PHP settings and I could keep going? Sometimes output is blank and sometimes its some JSON error. For e.g. see this text : NIFTY AS A DAY OF REST, GOES THROUGH AN INTRADAY CORRECTION, MARKETS REMAIN CHEERFUL – BUY ON DIPS If I do not change the "-" to something else the output is null/blank. My table are normal MyISAM engine MYSQL tables and field type is varchar. Quote Link to comment https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/#findComment-1579465 Share on other sites More sharing options...
requinix Posted July 10, 2020 Share Posted July 10, 2020 Alright, that's confirmation enough for me. You are not using UTF-8 right now. You need to be. There are a lot of places you will need to check to see what they're doing. It will take more than 5 minutes for you to fix this problem so don't rush it. This looks like a good resource. A lot of it applies to you. Some of it does not. Quote Link to comment https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/#findComment-1579466 Share on other sites More sharing options...
greatchap Posted July 10, 2020 Author Share Posted July 10, 2020 All I need to do was add this line -> mysqli_set_charset($con, "utf8"); Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/#findComment-1579478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.