2016-02-18 84 views
1

我使用的MySQL数据库,并整理utf8_general_ci和我想要从这个数据库中的一些数据,并将这些数据保存在阿拉伯语的语言,但我得到这样的:JSON格式编码阿拉伯语

{"newstitle":"??????? ????? ?? ???? ???? ???? ??? ???? ????? ???? ??? ?? ???? ?????? ???????\r\n","image":"http:\/\/new.el-ahly.com\/Admin\/Sitemanager\/ArticleFiles\/83537-ron.jpg","newstext":"????? ???? ???? ????? ???????? ?? ????? ?????? ???? ???????? ?? ???? ??????? ??? ?????? ?? ??? ???16 ?????? ???? ????? ??????? ????? ????? ????????? ????????? ??????? ???? ???? ????? ?? ????????? ???????? ??? ????? ??? ???? ?????? ??????? ???? ????? ???? ??????? ?? ?????? .\r\n \r\n??? ??????? ??? ??? ???????? ????? ??? ????? ??? ??? ??????? ?????? ?? ?????? ???? ????? ???? ???? ??????? ?????? ??? ?????? ?????? ????? ?? ????? ??? ???? ???????? ??? ??? ??? ???? ????? ??? ???? ???? ?????? ???? ?? ????","time_stamp":"about 5 minutes ago","news_url":"https:\/\/goo.gl\/U33b0h"} 

这是我的PHP脚本:

<?php 
function timeago($ptime) 
{ 
    $estimate_time = time() - $ptime; 

    if($estimate_time < 1) 
    { 
     return 'less than 1 second ago'; 
    } 

    $condition = array( 
       12 * 30 * 24 * 60 * 60 => 'year', 
       30 * 24 * 60 * 60  => 'month', 
       24 * 60 * 60   => 'day', 
       60 * 60     => 'hour', 
       60      => 'minute', 
       1      => 'second' 
    ); 

    foreach($condition as $secs => $str) 
    { 
     $d = $estimate_time/$secs; 

     if($d >= 1) 
     { 
      $r = round($d); 
      return 'about ' . $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago'; 
     } 
    } 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$db = "finaldatabase"; 

$conn = mysqli_connect($servername, $username, $password, $db); 

if(!$conn) 
{die ("Connection failed");} 

$query = "select * from playernews"; 

$result = mysqli_query($conn, $query); 
$rows = array(); 

while($row = mysqli_fetch_assoc($result)) { 
    $News_Titles[]=$row['NewsTitle']; 
    $Images[]=$row['image']; 
    $News_Text[]=$row['NewsText']; 
    $Time_Stamp[]=$row['timeStamp']; 
    $News_Url[]=$row['url']; 

    //echo $row['url']; 
} 

// sleep for 2 sec show that the androd swipe refresh will be visible for sometime 
sleep(2); 

// reading offset from get parameter 
$offset = isset($_GET['offset']) && $_GET['offset'] != '' ? $_GET['offset'] : 0; 

// page limit 
$limit = 20; 
//$offset=0; 


$movies_array = array(); 

//$rows=array_reverse($rows); 

// loop through page movies 
for ($j = $offset; $j < $offset + $limit && $j < sizeof($Images); $j++) { 
    $tmp = array(); 
    //$tmp['rank'] = $j + 1; 
    $tmp['newstitle'] = $News_Titles[$j]; 
    $tmp['image']=$Images[$j]; 
    $tmp['newstext']=$News_Text[$j]; 
    $time_ago =strtotime($Time_Stamp[$j]); 

    $tmp['time_stamp']=timeAgo($time_ago); 
    $tmp['news_url']=$News_Url[$j]; 

    array_push($movies_array, $tmp); 
} 

//$reverse = array_reverse($movies_array, true); 

// printing json response 
echo json_encode($movies_array); 

$curenttime="2016-02-18 22:29:13"; 
    $time_ago =strtotime($curenttime); 
    //echo timeAgo($time_ago); 

$myfile = fopen("movies.json", "w") or die("Unable to open file!"); 
$txt = json_encode($movies_array); 
fwrite($myfile, $txt); 
fclose($myfile); 

//header("Location: movies.json"); /* Redirect browser */ 
exit(); 
?> 

我应该如何处理这个问题?

回答

0
$conn=mysqli_connect($servername,$username,$password,$db); 
$this->mysqli->set_charset('utf8mb4'); // add this line 

utf8_general_ci很烂,使用utf8mb4_unicode_ci代替