{"id":1118,"date":"2021-11-04T00:56:02","date_gmt":"2021-11-03T16:56:02","guid":{"rendered":"http:\/\/www.tra56.com\/?p=1118"},"modified":"2021-11-04T00:56:02","modified_gmt":"2021-11-03T16:56:02","slug":"1118","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/1118.html","title":{"rendered":"PHP for SQLSrv \u64cd\u4f5c\u7c7b"},"content":{"rendered":"

\u64cd\u4f5c\u7c7b<\/p>\n

<?php\r\n \r\n\/**\r\n* SqlServer\u64cd\u4f5c\u7c7b(sqlsrv)\r\n* Class SQLSrv\r\n*\/\r\nclass SQLSrv\r\n{\r\n   private $dbhost;\r\n   private $dbuser;\r\n   private $dbpw;\r\n   private $dbname;\r\n   private $port;\r\n   private $result;\r\n   private $connid = 0;\r\n   private $insertid = 0;\r\n   private $cursor = 0;\r\n   public static $instance = null;\r\n \r\n   public function __construct($db)\r\n   {\r\n       function_exists("sqlsrv_connect") or die("<pre>\u8bf7\u5148\u5b89\u88c5 sqlsrv \u6269\u5c55\u3002");\r\n \r\n       $this->dbhost = !empty($db['hostname']) ? $db['hostname'] : 'localhost';\r\n       $this->dbuser = $db['username'];\r\n       $this->dbpw = $db['password'];\r\n       $this->dbname = $db['dbname'];\r\n       $this->port = !empty($db['port']) ? $db['port'] : 1433;\r\n       $this->connect();\r\n   }\r\n \r\n   public static function getdatabase($db){\r\n       if(empty(self::$instance)){\r\n           self::$instance = new SQLSrv($db);\r\n       }\r\n       return self::$instance;\r\n   }\r\n \r\n   \/**\r\n    * \u8fde\u63a5\u6570\u636e\u5e93\r\n    * @return int\r\n    *\/\r\n   private function connect()\r\n   {\r\n       $serverName = "{$this->dbhost}, {$this->port}";\r\n       $connectionInfo = array( "Database"=>$this->dbname, "UID"=>$this->dbuser, "PWD"=>$this->dbpw);\r\n       if(!$this->connid = @sqlsrv_connect($serverName, $connectionInfo)){\r\n           $this->halt(print_r( sqlsrv_errors(), true));\r\n       }\r\n \r\n       return $this->connid;\r\n   }\r\n \r\n   \/**\r\n    * \u6267\u884csql\r\n    * @param $sql\r\n    * @return mixed\r\n    *\/\r\n   public function query($sql)\r\n{\r\nif(empty($sql)){\r\n           $this->halt('SQL IS NULL!');\r\n}\r\n \r\n$result = sqlsrv_query($this->connid, $sql);\r\n \r\nif(!$result){  \/\/\u8c03\u8bd5\u7528\uff0csql\u8bed\u53e5\u51fa\u9519\u65f6\u4f1a\u81ea\u52a8\u6253\u5370\u51fa\u6765\r\n           $this->halt('MsSQL Query Error', $sql);\r\n}\r\n \r\n       $this->result = $result;\r\n \r\nreturn $this->result;\r\n}\r\n \r\n   \/**\r\n    * \u83b7\u53d6\u4e00\u6761\u6570\u636e\uff08\u4e00\u7ef4\u6570\u7ec4\uff09\r\n    * @param $sql\r\n    * @return array|bool\r\n    *\/\r\n   public function find($sql)\r\n   {\r\n       $this->result = $this->query($sql);\r\n       $args = $this->fetch_array($this->result);\r\n       return $args ;\r\n   }\r\n \r\n   \/**\r\n    * \u83b7\u53d6\u591a\u6761\uff08\u4e8c\u7ef4\u6570\u7ec4\uff09\r\n    * @param $sql\r\n    * @param string $keyfield\r\n    * @return array\r\n    *\/\r\n   public function findAll($sql, $keyfield = '')\r\n   {\r\n       $array = array();\r\n       $this->result = $this->query($sql);\r\n       while($r = $this->fetch_array($this->result)){\r\n           if($keyfield){\r\n               $key = $r[$keyfield];\r\n               $array[$key] = $r;\r\n           }else{\r\n               $array[] = $this->objectToArray($r);\r\n           }\r\n       }\r\n       return $array;\r\n   }\r\n \r\n   \/**\r\n    * \u5bf9\u8c61\u8f6c\u6570\u7ec4\r\n    * @param $obj\r\n    * @return array\r\n    *\/\r\n   private function objectToArray($obj){\r\n       $ret = array();\r\n       foreach ($obj as $key => $value) {\r\n           if (gettype($value) == "array" || gettype($value) == "object"){\r\n               $ret[$key] =  $this->objectToArray($value);\r\n           }else{\r\n               $ret[$key] = $value;\r\n           }\r\n       }\r\n       return $ret;\r\n   }\r\n \r\n   public function fetch_array($query, $type = SQLSRV_FETCH_ASSOC)\r\n   {\r\n       if(is_resource($query)) return sqlsrv_fetch_array($query, $type);\r\n       if($this->cursor < count($query)){\r\n           return $query[$this->cursor++]; \r\n       }\r\n       return FALSE; \r\n   }\r\n \r\n   public function affected_rows()\r\n   {\r\n       return sqlsrv_rows_affected($this->connid);\r\n   }\r\n \r\n   public function num_rows($query)\r\n   {\r\n       return is_array($query) ? count($query) : sqlsrv_num_rows($query);\r\n   }\r\n \r\n   public function num_fields($query)\r\n   {\r\n       return sqlsrv_num_fields($query);\r\n   }\r\n \r\n   \/**\r\n    * \u91ca\u653e\u8fde\u63a5\u8d44\u6e90\r\n    * @param $query\r\n    *\/\r\n   public function free_result($query)\r\n   {\r\n       if(is_resource($query)) @sqlsrv_free_stmt($query);\r\n   }\r\n \r\n   public function insert_id()\r\n   {\r\n       return $this->insertid;\r\n   }\r\n \r\n   public function fetch_row($query)\r\n   {\r\n       return sqlsrv_num_rows($query);\r\n   }\r\n \r\n   \/**\r\n    * \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\n    * @return bool\r\n    *\/\r\n   public function close()\r\n   {\r\n       return sqlsrv_close($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u629b\u51fa\u9519\u8bef\r\n    * @param string $message\r\n    * @param string $sql\r\n    *\/\r\n   public function halt($message = '', $sql = '')\r\n   {\r\n       $_sql = !empty($sql) ? "MsSQL Query:$sql <br>" : '';\r\n       exit("<pre>{$_sql}Message:$message");\r\n   }\r\n \r\n   \/**\r\n    * \u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1.\r\n    *\/\r\n   public function begin()\r\n   {\r\n       return sqlsrv_begin_transaction($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u63d0\u4ea4\u4e00\u4e2a\u4e8b\u52a1.\r\n    *\/\r\n   public function commit()\r\n   {\r\n       return sqlsrv_commit($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u56de\u6eda\u4e00\u4e2a\u4e8b\u52a1.\r\n    *\/\r\n   public function rollback()\r\n   {\r\n       return sqlsrv_rollback($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u8fd4\u56de\u670d\u52a1\u5668\u4fe1\u606f\r\n    * @return array\r\n    *\/\r\n   public static function serverInfo(){\r\n       return sqlsrv_server_info($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u8fd4\u56de\u5ba2\u6237\u7aef\u4fe1\u606f\r\n    * @return array|null\r\n    *\/\r\n   public static function clientInfo(){\r\n       return sqlsrv_client_info($this->connid);\r\n   }\r\n \r\n   \/**\r\n    * \u6790\u6784\u51fd\u6570,\u5173\u95ed\u6570\u636e\u5e93,\u5783\u573e\u56de\u6536\r\n    *\/\r\n   public function __destruct()\r\n   {\r\n       if(!is_resource($this->connid)){\r\n           return;\r\n       }\r\n \r\n       $this->free_result($this->result);\r\n       $this->close();\r\n   }\r\n}<\/pre>\n

2. [\u4ee3\u7801]\u4f7f\u7528\u65b9\u6cd5<\/span><\/p>\n

<?php\r\nrequire "database\/sqlsrv_driver.php";     \/\/\u5bfc\u5165\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\r\n \r\n$_db = array( \/\/\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\r\n   'hostname' => 'localhost', \/\/\u4e3b\u673a\u5730\u5740\r\n   'username' => 'sa', \/\/\u7528\u6237\u540d\r\n   'password' => '123', \/\/\u5bc6\u7801\r\n   'dbname' => 'test_db', \/\/\u6570\u636e\u5e93\u540d\r\n   'port' => 1433, \/\/\u7aef\u53e3  \u9ed8\u8ba41433\r\n);\r\n$db = SQLSrv::getdatabase($_db); \/\/\u6570\u636e\u5e93\u8fde\u63a5\r\n \r\n#\u6700\u7ec8 $db \u5c31\u662f\u4f60\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\r\n \r\n#\u67e5\u8be2\u793a\u4f8b\r\n$sql = "select * from table1";\r\n \r\n#\u67e5\u8be2\u5355\u6761\uff1a\r\n$result = $db->find($sql);\r\n \r\n#\u67e5\u8be2\u591a\u6761\uff1a\r\n$result = $db->findAll($sql);\r\n \r\n#\u6267\u884c\u589e\u5220\u6539\uff1a\r\n$result = $db->query($sql);\r\n \r\n#...<\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u64cd\u4f5c\u7c7b <?php   \/** * SqlServer\u64cd\u4f5c\u7c7b(sqlsrv) *&n […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-1118","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1118"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=1118"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1118\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1118\/revisions\/1119"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=1118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=1118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=1118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}