Jump to content

Recommended Posts

Dear all hello. I am facing a major issue with greek characters. Here is the php script:

 

<?php
$connect = mssql_connect($db_server,$db_username,$db_password) or die("0001");

mssql_select_db($wf_db) or die("0002");

$sql_a = "SELECT [iD],[WASTE_SN] FROM [Webforms].[dbo].[Waste_Types] ORDER BY [WASTE_SN] ASC";

$waste_name = array();
$waste_id   = array();

$query_a = mssql_query($sql_a) or die("0003");

while ($row = mssql_fetch_assoc($query_a)){

$waste_name[] = $row['WASTE_SN'];
$waste_id[]   = $row['ID'];

}

mssql_close($connect);

$final_array = array( "waste_name" => $waste_name,
				  "waste_id"   => $waste_id
				  );

$json = json_encode($final_array);
				  
echo $json;

?>

Json_encode returns null for all values of array $waste_name when greek words are found. When I use print_r($waste_name), i can see all words fine. I've tried almost everything, including utf8_encode without results. Any help will be really appreciated!

Link to comment
https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/
Share on other sites

If you have PHP 5.3 then try the $options parameter to json_encode(). But yes, the data has to be UTF-8 encoded.

$final_array = array(
"waste_name" => utf8_encode($waste_name),
"waste_id"   => $waste_id
);

So that doesn't work? There may be encoding issues but you should at least get something. If not, does json_last_error return anything helpful?

Thank you very much for your help but:

 

1.utf8_encode does not accept an array. Of course I've tried to apply it when I am retrieving data from the table but without success. UTF8 conversion works, but javascript cannot understand it. It is invalid.

2.No errors are found with json_last_error function.

 

Any other ideas?

I just wrote a quick example, and it seams okay, can you provide a bit more detail..

 

here is my example

 

<?php
$waste_name = array();
$waste_id = array();

$waste_name[] = "Αυτή ";
$waste_id[] = 1;
$waste_name[] = "είναι ";
$waste_id[] = 2;
$waste_name[] = "μια ";
$waste_id[] = 3;
$waste_name[] = "δοκιμή";
$waste_id[] = 4;
$waste_name[] = "Blar";
$waste_id[] = 5;


$final_array = array(
    "waste_name" => $waste_name,
    "waste_id" => $waste_id
);

$json = json_encode($final_array);
?><html>
<head><TITLE>testing</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<script>
  var JSONstring = <?php echo $json;?>;
  for(i in JSONstring.waste_id){
    document.write("<p>"+JSONstring.waste_id[i]+":"+JSONstring.waste_name[i]+"</p>");
  }
</script>
</head>
<body>
<p>test page</p>
</body>
</html>

Thanks for your help! I've just checked your email and it works fine. I believe that there is something wrong in the mssql or probably in SQL Server. I've modified the script to work with MySQL and it works perfectly without issues... Next step to investigate drivers and sql server charsets etc etc...

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.