1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Centos6.6 通过freetds pdo_dblib使用php-5.6.17连接sqlserver

Posted at

问题

使用pdo_odbc连接sqlserver发现对中文支持,不是太好。尤其是SQL server默认的字符集是GBK。
为了解决插入中文乱码,读取也会乱码的问题,变更连接方式。

测试是用freetds pdo_dblib组合

  • freetds已经包含在epel里,所以只要有epel的repo即可通过yum来安装。安装前提是,要首先安装了unixODBC包。
yum install freetds freetds-devel -y
  • 对pho_dblib编译

编译过程中,可能会出现找不到 /usr/lib/libsydbd.so|a 的提示。

其实so已经有了,不过已经不在/usr/lib里,而是安装到了/usr/lib64里边。所以可以做一个软链解决。

 sudo ln -s /usr/lib64/libsybdb.so /usr/lib/libsybdb.so

进入源码包目录的ext中,到pdo_dblib目录

cd php-5.6.17/ext/pdo_dblib/
/usr/local/bin/phpize
./configure --with-pdo-dblib=/usr --with-php-config=/usr/local/bin/php-config
make
sudo make install

编译完成后,就会在扩展目录生成文件 pdo_dblib.so

*让php增加扩展

vim php.ini

##add pdo_dblib.so
extension="pdo_dblib.so"

php代码样例

<?php
  try {
    $hostname = "testserver.sqlserver.com";
    $port = 1433;
    $dbname = "dbname";
    $username = "user";
    $pw = "pwd";
    $dbh = new PDO ("dblib:host=$hostname:$port;charset=UTF-8;dbname=$dbname","$username","$pw");

  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
  $stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
  unset($dbh); unset($stmt);
?>

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?