centos 6 PHP7 + MSSQL

YUM REPO 추가
1. epel
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

2. remi
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

기존 PHP 삭제
# yum remove php*

PHP 7 설치 (php 패키지는 개인의 용도에 맞게 설치)
# yum –enablerepo=remi install php70 php70-php php70-php-cli php70-php-common php70-php-gd php70-php-json php70-php-mbstring php70-php-mcrypt php70-php-mysqlnd php70-php-opcache php70-php-pear php70-php-xml php70-php-devel php70-php-imagick php-pdo

※ php.ini 파일은 /etc/opt/remi/php70/php.ini로 변경 됨

PHP 7에서 MSSQL로의 쿼리는 PDO를 통해 가능하며 접속 및 테스트 코드는 아래와 같음

”config.php”

<?
 // 반드시 DRIVER는 dblib로
 $_DB_INFO_MSSQL = array(
  "_DRIVER" => "dblib", 
  "_HOST" => "", 
  "_PORT" => "1433", 
  "_USER" => "", 
  "_PASS" => "", 
  "_DB" => ""
 );
?>

”MSSQL_SAMPLE.php”

<?
/*
* Project : MSSQL DBMS Class (PHP 7 PDO)
* Author : colorweb <enjoyteam@nate.com>
* Last : 2016-08-02
*/
class MSSQL extends PDO
{
    private $dns;
    private $engine; 
    private $host; 
    private $database; 
    private $user; 
    private $pass; 
    private $port; 
    private $_CONN; 
    private $_RESULT;
    
    public function __construct(){ 
        $this->engine = $GLOBALS["_DB_INFO_MSSQL"]["_DRIVER"]; 
        $this->host = $GLOBALS["_DB_INFO_MSSQL"]["_HOST"]; 
        $this->database = $GLOBALS["_DB_INFO_MSSQL"]["_DB"]; 
        $this->user = $GLOBALS["_DB_INFO_MSSQL"]["_USER"]; 
        $this->pass = $GLOBALS["_DB_INFO_MSSQL"]["_PASS"]; 
        $this->port = $GLOBALS["_DB_INFO_MSSQL"]["_PORT"]; 
        $this->dns = $this->engine.\':dbname=\'.$this->database.";host=".$this->host.":".$this->port; 
    } 

    function connect()
    {
        try {
            $this->_CONN = new PDO ($this->dns, $this->user, $this->pass);
        } catch (PDOException $e) {
            //$this->logsys .= "Failed to get DB handle: " . $e->getMessage() . "\\n";
            echo "Failed to get DB handle: " . $e->getMessage();
        }
    }

 function select($sql)
 {
  if(!$this->_CONN) { $this->connect(); }

  $_CONN = $this->_CONN;

  $this->_RESULT = $_CONN->query($sql);

  return $this->_RESULT;
 }

 function get_rows()
 {
  return $this->_RESULT->fetchAll();
 }

 function excute($sql)
 {
  if(!$this->_CONN) { $this->connect(); }

  $_CONN = $this->_CONN;

  $this->_RESULT = $_CONN->prepare($sql);
  $this->_RESULT->execute();

  return $this->_RESULT;
 }
}
?>

”MSSQL_TEST.php”

<?
include "config.php";
include "MSSQL_SAMPLE.php";

$_MSSQL = new MSSQL($_DEBUG);
$_MSSQL->connect();
$db_list = $_MSSQL->select("select * from [table_name]");
$db_data = $_MSSQL->get_rows();

// 전체 데이터를 가져와서 출력하는 예제 #1
for($i=0; $i<sizeof($db_data); $i++)
{
    echo $db_data[$i][FIELD_NAME];
}
?>

 

Comments are closed.