首页 > php教程 > PHP源码 > PHP for SQLSrv 操作类

PHP for SQLSrv 操作类

不言
发布: 2018-05-15 10:10:15
原创
4627 人浏览过

跳至 [1] [2] [全屏预览]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

<?php

 

/**

* SqlServer操作类(sqlsrv)

* Class SQLSrv

*/

class SQLSrv

{

   private $dbhost;

   private $dbuser;

   private $dbpw;

   private $dbname;

   private $port;

   private $result;

   private $connid = 0;

   private $insertid = 0;

   private $cursor = 0;

   public static $instance = null;

 

   public function __construct($db)

   {

       function_exists("sqlsrv_connect") or die("<pre class="brush:php;toolbar:false">请先安装 sqlsrv 扩展。");

 

       $this->dbhost = !empty($db[&#39;hostname&#39;]) ? $db[&#39;hostname&#39;] : &#39;localhost&#39;;

       $this->dbuser = $db[&#39;username&#39;];

       $this->dbpw = $db[&#39;password&#39;];

       $this->dbname = $db[&#39;dbname&#39;];

       $this->port = !empty($db[&#39;port&#39;]) ? $db[&#39;port&#39;] : 1433;

       $this->connect();

   }

 

   public static function getdatabase($db){

       if(empty(self::$instance)){

           self::$instance = new SQLSrv($db);

       }

       return self::$instance;

   }

 

   /**

    * 连接数据库

    * @return int

    */

   private function connect()

   {

       $serverName = "{$this->dbhost}, {$this->port}";

       $connectionInfo = array( "Database"=>$this->dbname, "UID"=>$this->dbuser, "PWD"=>$this->dbpw);

       if(!$this->connid = @sqlsrv_connect($serverName, $connectionInfo)){

           $this->halt(print_r( sqlsrv_errors(), true));

       }

 

       return $this->connid;

   }

 

   /**

    * 执行sql

    * @param $sql

    * @return mixed

    */

   public function query($sql)

{

if(empty($sql)){

           $this->halt(&#39;SQL IS NULL!&#39;);

}

 

$result = sqlsrv_query($this->connid, $sql);

 

if(!$result){  //调试用,sql语句出错时会自动打印出来

           $this->halt(&#39;MsSQL Query Error&#39;, $sql);

}

 

       $this->result = $result;

 

return $this->result;

}

 

   /**

    * 获取一条数据(一维数组)

    * @param $sql

    * @return array|bool

    */

   public function find($sql)

   {

       $this->result = $this->query($sql);

       $args = $this->fetch_array($this->result);

       return $args ;

   }

 

   /**

    * 获取多条(二维数组)

    * @param $sql

    * @param string $keyfield

    * @return array

    */

   public function findAll($sql, $keyfield = &#39;&#39;)

   {

       $array = array();

       $this->result = $this->query($sql);

       while($r = $this->fetch_array($this->result)){

           if($keyfield){

               $key = $r[$keyfield];

               $array[$key] = $r;

           }else{

               $array[] = $this->objectToArray($r);

           }

       }

       return $array;

   }

 

   /**

    * 对象转数组

    * @param $obj

    * @return array

    */

   private function objectToArray($obj){

       $ret = array();

       foreach ($obj as $key => $value) {

           if (gettype($value) == "array" || gettype($value) == "object"){

               $ret[$key] =  $this->objectToArray($value);

           }else{

               $ret[$key] = $value;

           }

       }

       return $ret;

   }

 

   public function fetch_array($query, $type = SQLSRV_FETCH_ASSOC)

   {

       if(is_resource($query)) return sqlsrv_fetch_array($query, $type);

       if($this->cursor < count($query)){

           return $query[$this->cursor++];

       }

       return FALSE;

   }

 

   public function affected_rows()

   {

       return sqlsrv_rows_affected($this->connid);

   }

 

   public function num_rows($query)

   {

       return is_array($query) ? count($query) : sqlsrv_num_rows($query);

   }

 

   public function num_fields($query)

   {

       return sqlsrv_num_fields($query);

   }

 

   /**

    * 释放连接资源

    * @param $query

    */

   public function free_result($query)

   {

       if(is_resource($query)) @sqlsrv_free_stmt($query);

   }

 

   public function insert_id()

   {

       return $this->insertid;

   }

 

   public function fetch_row($query)

   {

       return sqlsrv_num_rows($query);

   }

 

   /**

    * 关闭数据库连接

    * @return bool

    */

   public function close()

   {

       return sqlsrv_close($this->connid);

   }

 

   /**

    * 抛出错误

    * @param string $message

    * @param string $sql

    */

   public function halt($message = &#39;&#39;, $sql = &#39;&#39;)

   {

       $_sql = !empty($sql) ? "MsSQL Query:$sql <br>" : &#39;&#39;;

       exit("<pre class="brush:php;toolbar:false">{$_sql}Message:$message");

   }

 

   /**

    * 开始一个事务.

    */

   public function begin()

   {

       return sqlsrv_begin_transaction($this->connid);

   }

 

   /**

    * 提交一个事务.

    */

   public function commit()

   {

       return sqlsrv_commit($this->connid);

   }

 

   /**

    * 回滚一个事务.

    */

   public function rollback()

   {

       return sqlsrv_rollback($this->connid);

   }

 

   /**

    * 返回服务器信息

    * @return array

    */

   public static function serverInfo(){

       return sqlsrv_server_info($this->connid);

   }

 

   /**

    * 返回客户端信息

    * @return array|null

    */

   public static function clientInfo(){

       return sqlsrv_client_info($this->connid);

   }

 

   /**

    * 析构函数,关闭数据库,垃圾回收

    */

   public function __destruct()

   {

       if(!is_resource($this->connid)){

           return;

       }

 

       $this->free_result($this->result);

       $this->close();

   }

}

登录后复制

2. [代码]使用方法 跳至 [1] [2] [全屏预览]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?php

require "database/sqlsrv_driver.php";     //导入数据库操作类

 

$_db = array( //数据库连接信息

   &#39;hostname&#39; => &#39;localhost&#39;, //主机地址

   &#39;username&#39; => &#39;sa&#39;, //用户名

   &#39;password&#39; => &#39;123&#39;, //密码

   &#39;dbname&#39; => &#39;test_db&#39;, //数据库名

   &#39;port&#39; => 1433, //端口  默认1433

);

$db = SQLSrv::getdatabase($_db); //数据库连接

 

#最终 $db 就是你的数据库实例

 

#查询示例

$sql = "select * from table1";

 

#查询单条:

$result = $db->find($sql);

 

#查询多条:

$result = $db->findAll($sql);

 

#执行增删改:

$result = $db->query($sql);

 

#...

登录后复制


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板