IER506 Posted September 26, 2011 Share Posted September 26, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/ Share on other sites More sharing options...
requinix Posted September 26, 2011 Share Posted September 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/#findComment-1272960 Share on other sites More sharing options...
IER506 Posted September 27, 2011 Author Share Posted September 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/#findComment-1273163 Share on other sites More sharing options...
MadTechie Posted September 27, 2011 Share Posted September 27, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/#findComment-1273177 Share on other sites More sharing options...
IER506 Posted September 27, 2011 Author Share Posted September 27, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/247897-php-json_encode-with-greek-characterrs/#findComment-1273209 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.