【发布时间】:2013-09-14 21:38:26
【问题描述】:
我使用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?�o c?�o việc sử 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 谢谢
【问题讨论】:
-
这是输出到网络浏览器吗?您是否在输出
Content-type中设置了正确的字符集? stackoverflow.com/questions/905173/… -
@MichaelBerkowski 我在上面有
header("Content-Type: text/html; charset=utf-8");但不工作,使用iconv("Windows-1256", "UTF-8", "$col1")仍然不工作?我的专栏有Collation: SQL_Latin1_General_CP1_CI_AS, nvarchar(3000)? -
这可能已经回答了你的问题:[How to set charset for SQL Connection][1] [1]: stackoverflow.com/questions/1322421/…
标签: php sql sql-server utf-8