使用PHP从MSSQL数据库检索希腊字母 [英] Retrieve Greek letters from MSSQL database with PHP

查看:69
本文介绍了使用PHP从MSSQL数据库检索希腊字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在.Net中已经做到这一点,但是我对PHP还是有些陌生,可以使用一些帮助.我正在尝试从MSSQL数据库中提取PHP中的数据,其中一些是希腊字母.MSSQL列的数据类型为 nvarchar ,并且希腊字符在Management Studio中正确显示;但是,每当我使用php中的 sqlsrv_query 函数提取数据时,我都会得到很多问号.

I've done this before in .Net but I am somewhat new to PHP and could use some help. I'm trying to pull data in PHP from a MSSQL db, some of which is Greek letters. The data type of the MSSQL columns is nvarchar and the Greek characters are displayed correctly in management studio; however, whenever I pull the data with the sqlsrv_query function in php I get a bunch of question marks.

sqlsrv_connect 函数中,我尝试设置不起作用的 CharacterSet"=>" UTF-8 .
我已经尝试过 iconv(),但是不确定要使用哪个设置.
我尝试了 mb_detect_encoding(),它返回了 ASCIIASCII .

In sqlsrv_connect function I've tried setting CharacterSet"=>"UTF-8 which doesn't work.
I've tried iconv(), but am not sure which sets to use.
I tried mb_detect_encoding() and it returned ASCIIASCII.

以下是一些示例代码:

public function _getOrderItemDetails($orderItemIds,$productTypeId){
    $params = explode(',', $orderItemIds);

    $sql = "select od.*, ofs.Sort
            from orderDetail od
                left join orderFieldSort ofs on ofs.fieldName = od.name and productTypeId in ($productTypeId)
            where orderItemid in (".rtrim(str_repeat('?,', count($params)),',').")
            order by ofs.sort";

   return $this->_compileResults($sql,'OrderDetail',$params);;
}

private function _compileResults($sql,$classType,$params ){       
    $result = sqlsrv_query($this->conn,$sql,$params);

    $arrayResult = array();
    while($orderObject = sqlsrv_fetch_object($result,$classType)){
        array_push($arrayResult, $orderObject);
    }
    return $arrayResult;
}

欢迎提出任何建议.请帮忙!

Any suggestions are welcome. Please help!

推荐答案

我遇到了与您完全相同的问题,因此当我寻找解决方案时,我看到了您的问题.答案非常简单.我猜您的某个地方您使用以下代码连接到数据库的代码:

I had exactly the same problem with you so when i was searching to find the solution i saw your question.The answer is very simple.I guess somewhere in your code you connect to the database with code like the following :

$serverName = "YOUR_SERVER";
$connectionInfo = array("Database"=>"YourDatabase");
$link = sqlsrv_connect($serverName,$connectionInfo);
if(!$link ) {
    die('Could not connect: ' . mysql_error());
}

要显示希腊字符,您需要从以下位置更改第2行:

To make greek characters show up you need to change line 2, from:

$connectionInfo = array("Database"=>"YourDatabase");

至:

$connectionInfo = array("Database"=>"YourDatabase","CharacterSet"=>"UTF-8");

,其余代码保持不变.@Graham针对我自己的问题提出了这个解决方案,它来自这里:

and the rest code stays the same. This solution was suggested by @Graham to his own question i think, and it is from here : Greek character insertion in php compared to SQL server management studio

这篇关于使用PHP从MSSQL数据库检索希腊字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆