3

I'm Trying to fetch some data from sql server database and print in json , but some of the data in database is in UTF-8 (Arabic) . I searched the web and tried many solutions , but none of them worked for me , any help would be appreciated . Here is my code :

<?php

$serverName = "127.0.0.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"filmnak_com_", "CharacterSet" => "UTF-8", "UID"=>"filmnak", "PWD"=>"G5j^wk44");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

$sql = "SELECT CAST(ID AS INT) AS ID, CAST(Film AS TEXT) AS Film ,CAST(Name AS TEXT) Name FROM dbo.Movies";

$result = sqlsrv_query( $conn, $sql);
if( $result === false ) {
     die( print_r( sqlsrv_errors(), true));
}

    // looping through all results
    // products node
    $response["Info"] = array();
  while ($row = sqlsrv_fetch_array($result)) {
        // temp user array
        $Info = array();
        $Info["ID"] = $row["ID"] ;
        $Info["Film"] = $row["Film"];

      $Info["Name"] = utf8_decode($row["Name"]);

    echo $row["Name"];
        // push single product into final response array
        array_push($response["Info"], $Info);
    }


echo json_encode($response , JSON_UNESCAPED_UNICODE);

sqlsrv_free_stmt($result);


?>

the result in browser :

???? ????1+1?????{"Info":[{"ID":8,"Film":"8O9","Name":"???? ????"},{"ID":9,"Film":"9O3","Name":"1+1"},{"ID":10,"Film":"10O5","Name":"?????"}]}
8
  • 1
    are you set collation ?? like this Arabic_CI_AI Commented Nov 12, 2016 at 12:25
  • 1
    + use the type nvarchar (or other similar like ntext, nchar Commented Nov 12, 2016 at 12:28
  • utf8_decode never does what you think it does. If your data is UTF 8 in the database and your connection charset is UTF 8 then PHP will receive it as UTF 8. PHP can handle UTF-8 just fine, no need for any special operations on them. Commented Nov 12, 2016 at 12:30
  • Possible duplicate of UTF-8 all the way through Commented Nov 12, 2016 at 12:31
  • @apokryfos , the data in database is UTF-8 , I set the characterset to UTF-8 , but when i use echo to print them , it will show question marks (?????) , ( echo $row["Name"]; gives question marks , utf8_decode($row["Name"]); gives question marks ) Commented Nov 12, 2016 at 12:58

3 Answers 3

19

USE THIS CODE

    $serverName="(local)";
    $connetionInfo = array("DataBase"=>"your db","UID"=>"username","PWD"=>"pass",
                           "CharacterSet" => "UTF-8");<---------------this
    $this->dblink=sqlsrv_connect($serverName,$connetionInfo);
Sign up to request clarification or add additional context in comments.

Comments

3

You'll need to specify the character set when connecting to the sqlsrv server. You can do this by adding the CharacterSet parameter like this:

$serverName = "Your server name";
$connectionInfo = [ "Database"=>"your db",  "CharacterSet" =>"UTF-8"];
$conn = sqlsrv_connect( $serverName, $connectionInfo);

Comments

1

Set the database collation to utf8_general_ci

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.