MSSQL使用PHP检索UTF-8 json数据

时间:2014-04-14 19:56:29

标签: php json utf-8 arabic ms-query

您好我正在尝试使用php从我的数据库中检索阿拉伯数据,但我得到的只是问号。数据存储在数据库中,数据类型为nvarchar

我试图将此代码ini_set('mssql.charset', 'UTF-8');放在我的php中,但它仍无法正常工作

以下是我的代码示例: 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');
        }



?>

1 个答案:

答案 0 :(得分:0)

试试这个

<?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);

?>