[英]PHP connect SQL server 2008 - How set utf-8 using odbc_connect
我使用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? 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謝謝
odbc_exec不接受“ SET NAMES utf8”作為第二個參數。 第二個參數必須是查詢。
為變量設置utf8 僅使用utf8_decode或iconv
$col1=utf8_decode(odbc_result($rs, "name"));
要么
$col1=odbc_result($rs, "name");
iconv("UTF-8", "CP1252", $col1);
和
警告:odbc_exec():SQL錯誤:[Microsoft] [SQL Server Native Client 10.0] [SQL Server]“名稱”不是可識別的SET選項。SQLExecDirect中的SQL狀態37000位於C:\\ xampp \\ htdocs \\ sql \\ index .php行32
這不是錯誤,是警告。 但是請檢查odbc_exec手冊以確保所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.