简体   繁体   English

PHP连接SQL Server 2008-如何使用odbc_connect设置utf-8

[英]PHP connect SQL server 2008 - How set utf-8 using odbc_connect

I using ODBC to connect sql server 2008 like 我使用ODBC连接sql server 2008之类的

$virtual_dsn = 'DRIVER={SQL Server};SERVER=MyServerName;DATABASE=myDatabase';
$conn = odbc_connect($virtual_dsn,'sa','mypass') or die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);

if (!$conn){
    if (phpversion() < '4.0'){
      exit("Connection Failed: . $php_errormsg" );
    }
    else{
      exit("Connection Failed:" . odbc_errormsg() );
    }
}

// This query generates a result set with one record in it.
$sql="SELECT TOP 10 * FROM Mytable";

# Execute the statement.
$rs=odbc_exec($conn,$sql);

// Fetch and display the result set value.
if (!$rs){
    exit("Error in SQL");
}
while (odbc_fetch_row($rs)){


    $col1=odbc_result($rs, "name");
    echo "$col1 <br>";

}

// Disconnect the database from the database handle.
odbc_close($conn);

But i get text not correct like 但是我收到的文字不正确

b? oc? o việc sử dụng

i try to using odbc_exec($conn, "SET names utf8"); 我尝试使用odbc_exec($conn, "SET names utf8"); but get error 但出现错误

 Warning: odbc_exec(): SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]'names' is not a recognized SET option., SQL state 37000 in SQLExecDirect in C:\xampp\htdocs\sql\index.php on line 32

How set utf-8 using odbc_connect thanks 如何使用odbc_connect设置utf-8谢谢

odbc_exec doesn't accept 'SET NAMES utf8' as second parameter. odbc_exec不接受“ SET NAMES utf8”作为第二个参数。 the second parameter must be the query. 第二个参数必须是查询。

to set utf8 for variables only use utf8_decode or iconv 为变量设置utf8 使用utf8_decodeiconv

$col1=utf8_decode(odbc_result($rs, "name"));

or 要么

$col1=odbc_result($rs, "name");
iconv("UTF-8", "CP1252", $col1);

and

Warning: odbc_exec(): SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]'names' is not a recognized SET option., SQL state 37000 in SQLExecDirect in C:\\xampp\\htdocs\\sql\\index.php on line 32 警告:odbc_exec():SQL错误:[Microsoft] [SQL Server Native Client 10.0] [SQL Server]“名称”不是可识别的SET选项。SQLExecDirect中的SQL状态37000位于C:\\ xampp \\ htdocs \\ sql \\ index .php行32

this is not an error, is a WARNING. 这不是错误,是警告。 but check odbc_exec manual to ensure all. 但是请检查odbc_exec手册以确保所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM