MS SQL + PHP issue displaying UTF-8 characters

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
josephchoufani
Forum Newbie
Posts: 3
Joined: Sun Apr 26, 2009 5:31 am

MS SQL + PHP issue displaying UTF-8 characters

Post by josephchoufani »

Hi,

I am having a problem displaying arabic characters from Microsoft SQL Server database.

I am using the following:
Apache 2.2.8 for windows + PHP 5.2.6 for windows + Microsoft SQL Server 2005 Express + Microsoft SQL Server 2005 Driver for PHP

The characters are being displayed as ???? in the web application. I tried to change the encoding with no success.

what I'm looking for is something similar to MySQL's: SET NAMES 'utf8'

Sample Code:

Code: Select all

<?php header("Content-type: text/html; charset=UTF-8"); ?>
<pre><?php
$serverName = '[Server]';
$connectionInfo = array( "Database"=>"[Database]");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
   echo "Connection established.\n";
   $rslt = sqlsrv_query($conn, 'SELECT * FROM [Table]');
   while($r = sqlsrv_fetch_array($rslt,SQLSRV_FETCH_ASSOC)) print_r($r);
}
else
{
   echo "Connection could not be established.\n";
   die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);
echo "Connection closed!";
?></pre>
Sample Output:

Code: Select all

Connection established.
Array
(
    [ID] => 10
   

Code: Select all

=> 01
    [AName] => ????
)
Array
(
    [ID] => 11
   

Code: Select all

=> ? 
    [AName] => ????
)
Array
(
    [ID] => 12
   

Code: Select all

=> ? 
    [AName] => ?????? 
)
Array
(
    [ID] => 13
   

Code: Select all

=> ? 
    [AName] => ????? ????? ? ????? ????? ? ???? ??????? 
)
Array
(
    [ID] => 14
   

Code: Select all

=> ? 
    [AName] => ?????? ???????
)
Array
(
    [ID] => 15
   

Code: Select all

=> ? 
    [AName] => ?????? ????????
)
Array
(
    [ID] => 16
   

Code: Select all

=> ? 
    [AName] => ???? ???????
)
Array
(
    [ID] => 17
   

Code: Select all

=> ? 
    [AName] => ?????? 
)
Array
(
    [ID] => 18
   

Code: Select all

=> ??
    [AName] => ?????? ?????
)
Array
(
    [ID] => 21
   

Code: Select all

=> 00
    [AName] => ?????? ??? ???? ???? ????
)
Array
(
    [ID] => 22
   

Code: Select all

=> ??
    [AName] => ????
)
Connection closed!
Thanks
Last edited by Benjamin on Sun Apr 26, 2009 12:07 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
josephchoufani
Forum Newbie
Posts: 3
Joined: Sun Apr 26, 2009 5:31 am

Re: MS SQL + PHP issue displaying UTF-8 characters

Post by josephchoufani »

I figured it out, I used the ADODB COM:

Code: Select all

$conn = new COM("ADODB.Connection", NULL, CP_UTF8);
Reference: http://bugs.php.net/bug.php?id=18169

Cheers
Post Reply