Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/311056-json-error-if-table-field-has-dash/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.