我用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_decode或iconv
$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