数据库
首页 > 数据库> > PHP在linux下 连接MSSQL

PHP在linux下 连接MSSQL

作者:互联网

<?php
$serverName = "192.168.2.251,2434";
$connectionInfo = array( "Database"=>"jfdf40", "UID"=>"sa", "PWD"=>"932kj");
$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就可以了

标签:contents,sqlsrv,comments,echo,stmt,linux,PHP,MSSQL,conn
来源: https://www.cnblogs.com/BTag/p/15826181.html