Linux php如何连接sql server 全过程

2017-12-15 16:30:24 admin ...

一、安装mssql扩展

环境:php5.6.32 centos6.8

1、下载

wget http://hk1.php.net/get/php-5.6.32.tar.gz

2、解压

tar zxvf php-5.6.32.tar.gz

3、进入扩展目录

cd php-5.6.32/ext/mssql

4、安装依赖

yum install freetds freetds-devel

5、安装扩展

/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config --with-libdir=lib64
make
make install

6、添加扩展到php.ini

vim /usr/local/php/etc/php.ini

底部新增

extension=mssql.so

7、重启php-fpm

service php-fpm restart

二、PHP连接example

<?php
    $server = "";  //server name    = "welcome"
    $user   = "";  //user name      = "std"
    $pass   = "";  // password      = "3434&^%"
    $mydb   = "";  //database name  = "std"
    $con = mssql_connect($server, $user, $pass) or die("Couldn't connect to MSSQL Server on $server");
    $db =  mssql_select_db($mydb, $con) or die("Couldn't open database $mydb");
    $sql = "SELECT * FROM [jdp_tb_trade] WHERE [created] >= '2017-12-14 00:00:00'";
    $all = mssql_query($sql,$con);
    $tables = [];
    while($fet_tbl = mssql_fetch_array($all)) { // PUSH ALL TABLES AND COLUMNS INTO THE ARRAY
            foreach($fet_tbl as $k=>&$v ){
                    $v = mb_convert_encoding($v,"UTF-8", "GB2312");
            }
            $tables[] = $fet_tbl;
    }
    print_r($tables);

三、总结遇到的坑

  • 1、configure的时候不要忘记—with-libdir=lib64
  • 2、转码使用函数mb_convert_encoding($v,”UTF-8”, “GB2312”);

相似文章


345708673

在路上

技术无止境,一直在路上。 年过而立,惴惴不安,愈加发奋,孜孜求学。