1

Hi i am trying to retrieve Arabic data from my database using php but all i got is questions marks .. the data stored fine in the database and the column type is nvarchar

I tried to put this code ini_set('mssql.charset', 'UTF-8'); in my php but still it is not working

Here is sample of my code: db_connect.php:

<?php

ini_set('mssql.charset', 'UTF-8');

$link=mssql_connect('SQL*******', 'username', 'password');

?>

get_about.php

<?php

        include_once './db_connect.php';
        $sql ="SELECT * FROM About"; 
        $select = mssql_query($sql);

        if ($select) {
            while ($list = mssql_fetch_array($select)){
                $output = $list;
            }
            print json_encode($output);
        } else {
            print json_encode('fail select');
        }



?>
FinalDark
  • 1,748
  • 1
  • 16
  • 26
  • Is the data wrong completely? (Without converting to JSON) – mark.sagikazar Apr 14 '14 at 20:09
  • This answer may be of value to you: http://stackoverflow.com/a/13377935/647621 – bigmike7801 Apr 14 '14 at 20:22
  • @mark.sagikazar even without enconding it to json still i got questions mark when retrieving Arabic data – FinalDark Apr 15 '14 at 06:44
  • @bigmike7801 i tried the mb_convert_encoding but still not working – FinalDark Apr 15 '14 at 06:59
  • @FinalDark Firstly, try using PDO drivers instead. `mssql_*` functions are deprecated. Secondly, I found [this](http://stackoverflow.com/questions/11095379/mssql-php-cant-select-field-type-nvarcharmax). A commenter also mentions, that there is an ODBC driver for MSSQL, which can be used with PHP. Try that too, but the old drivers are outdated. – mark.sagikazar Apr 15 '14 at 08:09
  • @bigmike7801 Question mark means there is no real data there. Also, the question clearly mentions, that he uses FreeTDS, that is also a different thing. – mark.sagikazar Apr 15 '14 at 08:12
  • i tried using PDO instead of MSSQL for connecting data base but it gave me MySQL Error (10060) .. i think because the Database is MSSQL and it can only connect to MySQL database ..... also i am using smarterasp as a hosting website – FinalDark Apr 16 '14 at 07:56

1 Answers1

0

Try this

<?php
$user = "user";
$pass = "Password";

//11.0 for SQL 2012,2014

$dsn = "Driver=SQL Server Native Client 11.0;Server=server.com;Port=1433;Database=database;";
$cx = odbc_connect($dsn,$user,$pass);

// Get the error message

if($cx === false) {
    throw new ErrorExcpetion(odbc_errormsg());
};
$resultset=odbc_exec($cx, "SELECT * FROM Table");


$json = array();

do {
     while ($row = odbc_fetch_array($resultset)) {
     $json[] = $row;
     }
} while ( odbc_next_result($resultset) );

/* Run the tabular results through json_encode() */
/* And ensure numbers don't get cast to trings */
echo json_encode($json);

odbc_close( $cx);

?>
Hussain Nasif
  • 101
  • 10