简体   繁体   中英

Retrieve Chinese language data from SQL Server using PHP?

I want to retrieve Chinese language Data store in SQL Server using PHP. How I can do that as I try with the below code but it is not working. it is just returning ???????????

My code:

$query = "SELECT                   
               [name]
              ,[venue]                
              FROM tablename
              WHERE id = '2'";

/**********************************************/
//Just for the Purpose of Count Number of Rows
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
/******************************/

#Query the database to get the user details. 
$res = sqlsrv_query($conn, $query, $params, $options);  

$arr = array();
#If no data was returned, check for any SQL errors 
if ($res == false) 
{ 
   echo 'Query not Executed : '.  die(print_r(sqlsrv_errors(), TRUE));
}
else
{       
    while($obj = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC))
    {
        $arr[] = array(
        "name" => $obj['name'],
        "venue" => $obj['venue']
        );
    }
}

header('Content-type: application/json; charset=utf-8');
#Output the JSON data 
echo json_encode($arr);
exit();


sqlsrv_free_stmt($res); // Closes a statement. Frees all resources associated.
sqlsrv_close($conn);    // Closes a connection. Frees all resources associated.

My database table structure:
在此处输入图片说明

After Grabbing my mind a lot then finally i found the Solution My Self and the Solution is just Add Character Set option in your Connection String.

For Example:

$result = sqlsrv_connect($hostname, array(
        'UID' => $username,
        'PWD' => $password,
        'Database' => $database,
        "CharacterSet" => "UTF-8"      
));

So Just Add "CharacterSet" => "UTF-8"

hope it will solve the issue.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM