PHP串連SQLServer2012兩例

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   art   for   

首先放上

PHP串連SQLServer的驅動

http://php.net/manual/zh/ref.pdo-sqlsrv.php

另外PHP for IIS管理工具 大家可以自己搜尋一下 很久以前下的。現在忘記地址了。

PHPManagerForIIS-1.1.2-x64.msi

 

第一個是比較基礎的,直接建立conn 串連原生SQLExpress執行個體

    $conn = new PDO("sqlsrv:server=.\SQLExpress;Database=thinkphp","admin","pass1234");    //$conn = new PDO("sqlsrv:Server=127.0.0.1/Express; Database=MyeCharts", "admin", "pass1234");     $query = ‘select top 1 * from think_data‘;      $stmt = $conn->query( $query );      while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {         //print_r( $row );      }  

第二個是網上搜羅到的建立了類來控制 串連遠程伺服器。

class mssql{        private $host;        private $username;        private $password;        private $database;        private $handle;        private function __construct (Array $config){            $this -> host = $config[‘DB_HOST‘];            $this -> username = $config[‘DB_USER‘];            $this -> password = $config[‘DB_PWD‘];            $this -> database = $config[‘DB_NAME‘];            $this -> init();        }        private function init() {            $dsn = ‘sqlsrv:server = ‘.$this -> host.‘;database = ‘.$this->database;            $this -> handle = new PDO($dsn,$this -> username, $this -> password);        }        public static function GetInstance(array $config = null) {            if (null == $config) {                return NULL;            }             static $db = null;            if (null == $db) {                $c = __CLASS__;                $db = new $c($config);            };            return $db -> handle;        }    }

調用方法

    $config = array(        ‘DB_HOST‘ => ‘servername,1800‘,        ‘DB_NAME‘ => formname,        ‘DB_USER‘ => ‘admin‘,        ‘DB_PWD‘ => ‘pass1234‘    );    $mssql = mssql::GetInstance($config);    $result = $mssql->query(‘SELECT MAX(formID) FROM fromname);    foreach($result as  $row){        $id = $row[0];        echo $id;    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.