Home > php教程 > PHP源码 > PHP for MsSQL operation class

PHP for MsSQL operation class

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 08:53:39
Original
1415 people have browsed it
Jump to [1] [2] [Full screen preview]

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

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

<?php

 

/**

 * SqlServer操作类(mssql)

 * Class MsSQL

 */

class MsSQL

{

    private $dbhost;

    private $dbuser;

    private $dbpw;

    private $dbname;

    private $pconnect;

    private $result;

    private $querynum = 0;

    private $connid = 0;

    private $insertid = 0;

    private $cursor = 0;

    public static $instance = null;

 

    public function __construct($db)

    {

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

 

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

        $this->dbuser = $db['username'];

        $this->dbpw = $db['password'];

        $this->dbname = $db['dbname'];

        $this->pconnect = !empty($db['pconnect']) ? $db['pconnect'] : 0;

        $this->connect();

    }

 

    public static function getdatabase($db){

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

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

        }

        return self::$instance;

    }

 

    /**

     * 连接数据库

     * @return int

     */

    private function connect()

    {

        $func = $this->pconnect == 1 ? 'mssql_pconnect' : 'mssql_connect';

        if(!$this->connid = @$func($this->dbhost, $this->dbuser, $this->dbpw)){

            $this->halt('Can not connect to MsSQL server');

        }

         

        if(!empty($this->dbname)){

            if(!$this->select_db($this->dbname)){

                $this->halt('Cannot use database '.$this->dbname);

            }

        }

 

        return $this->connid;

    }

 

    /**

     * 选择数据库

     * @param $dbname

     * @return bool

     */

    public function select_db($dbname)

    {

        return @mssql_select_db($dbname , $this->connid);

    }

 

    /**

     * 直接执行sql

     * @param $sql

     * @return mixed

     */

    public function query_simple($sql)

    {

        if(empty($sql)){

            $this->halt('SQL IS NULL!');

        }

 

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

 

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

            $this->halt('MsSQL Query Error', $sql);

        }

 

        $this->result = $result;

 

        return $this->result;

    }

 

    /**

     * 过滤执行sql

     * @param $sql

     * @return array|mixed

     */

    public function query($sql)

    {

        $this->querynum++;

        $sql = trim($sql);

        if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs)){

            $sql = $matchs[1];

            $offset = $matchs[2];

            $pagesize = $matchs[4];

            $this->result = mssql_query($sql, $this->connid) or $this->halt('MsSQL Query Error', $sql);

            return $this->limit($this->result, $offset, $pagesize);

        }elseif(preg_match("/^insert into/i", $sql)){

            $sql = "$sql; Select @@identity as insertid";

            $this->result = mssql_query($sql, $this->connid) or $this->halt('MsSQL Query Error', $sql);

            $insid = $this->fetch_row($this->result);

            $this->insertid = $insid[0];

            return $this->result;

        }else{

            $this->result = mssql_query($sql, $this->connid) or $this->halt('MsSQL Query Error', $sql);

            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 = '')

    {

        $array = array();

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

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

            if($keyfield){

                $key = $r[$keyfield];

                $array[$key] = $r;

            }else{

                $array[] = $r;

            }

        }

        return $array;

    }

 

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

    {

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

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

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

        }

        return FALSE;

    }

 

    public function affected_rows()

    {

        return mssql_rows_affected($this->connid);

    }

 

    public function num_rows($query)

    {

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

    }

 

    public function num_fields($query)

    {

        return mssql_num_fields($query);

    }

 

    public function get_result($query, $row)

    {

        return @mssql_result($query, $row);

    }

 

    /**

     * 释放连接资源

     * @param $query

     */

    public function free_result($query)

    {

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

    }

 

    public function insert_id()

    {

        return $this->insertid;

    }

 

    public function fetch_row($query)

    {

        return mssql_fetch_row($query);

    }

 

    /**

     * 关闭数据库连接

     * @return bool

     */

    public function close()

    {

        return mssql_close($this->connid);

    }

 

    /**

     * 抛出错误

     * @param string $message

     * @param string $sql

     */

    public function halt($message = '', $sql = '')

    {

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

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

    }

 

    public function limit($query, $offset, $pagesize = 0)

    {

        if($pagesize > 0){

            mssql_data_seek($query, $offset);

        }else{

            $pagesize = $offset;

        }

 

        $info = array();

        for($i = 0; $i < $pagesize; $i++){

            $r = $this->fetch_array($query);

            if(!$r) break;

            $info[] = $r;

        }

 

        $this->cursor = 0;

        return $info;

    }

 

    /**

     * 初始化存储过程

     * @param $proNme

     * @return resource

     */

    public function init_pro($proNme)

    {

        return mssql_init($proNme, $this->connid);

    }

 

    /**

     * 开始一个事务.

     */

    public function begin()

    {

        $this->query('begin tran');

    }

 

    /**

     * 提交一个事务.

     */

    public function commit()

    {

        $this->query('commit tran');

    }

 

    /**

     * 回滚一个事务.

     */

    public function rollback()

    {

        $this->query('rollback tran');

    }

 

    /**

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

     */

    public function __destruct()

    {

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

            return;

        }

 

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

        $this->close();

    }

}

Copy after login

2. [Code]How to use Jump to [1] [2] [Full screen preview]

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

<?php

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

 

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

    'hostname' => 'localhost', //主机地址

    'username' => 'sa', //用户名

    'password' => '123', //密码

    'dbname' => 'test_db', //数据库名

);

 

#如果是linux,端口号不是默认1433,那么hostname就是 localhost:端口号

 

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

 

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

 

#查询示例

$sql = "select * from table1";

 

#查询单条:

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

 

#查询多条:

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

 

#执行增删改:

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

 

#...

Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template