PHP在linux下 连接MSSQL
<?php $serverName = "192.168.2.251,2434"; $connectionInfo = array( "Database"=>"jfdf40", "UID"=>"sa", "PWD"=>"XXX"); $conn = sqlsrv_connect( $serverName, $connectionInfo ); if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } //$stmt = sqlsrv_query( $conn, 'SET TEXTSIZE 80000' ); $sql = "SELECT msg FROM test"; $stmt = sqlsrv_query( $conn, $sql ); if( $stmt === false) { die( print_r( sqlsrv_errors(), true) ); } while ( sqlsrv_fetch( $stmt)) { $comments = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)); //echo get_resource_type ($comments); $contents = ''; while (!feof($comments)) { $contents .= fread($comments, 8192); } $s = (mb_convert_encoding(($contents), 'utf-8', 'gbk')); echo $s . PHP_EOL; echo mb_strlen($s); echo "\n"; }
mssql 一般在我们中国默认排序规则都是 Chinese_PRC_CI_AS
查询用,得到代码页是963=BGK
sELECT DATABASEPROPERTYEX('databasename', 'Collation') SQLCollation; SELECT COLLATIONPROPERTY('Chinese_PRC_CI_AS', 'CodePage')
php在linux 下连接mssql 读取中文会有问题,这里采用流读取的方式以2进制形式读回来。再把gbk转成utf-8就可以了