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

前端之家收集整理的这篇文章主要介绍了PHP连接SQL Server 2008 – 如何使用odbc_connect设置utf-8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用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);

但我得到的文字不正确

b? oc? oviệcsửdụng

我尝试使用odbc_exec($conn,“SET names utf8”);
但得到错误

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_connect感谢设置utf-8

odbc_exec不接受’SET NAMES utf8’作为第二个参数.第二个参数必须是查询.

为变量设置utf8仅使用utf8_decodeiconv

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

要么

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

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手册以确保全部.

猜你在找的PHP相关文章