ホームページ php教程 PHP源码 phpcmsv9 数据库操作类mysqli支持php7版本

phpcmsv9 数据库操作类mysqli支持php7版本

May 23, 2016 am 08:39 AM

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

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

<?php

/**

 *  mysql.class.php 数据库实现类

 *

 * @copyright           (C) 2005-2010 PHPCMS

 * @license             http://www.phpcms.cn/license/

 * @lastmodify          2010-6-1

 */

  

final class Database {

      

    /**

     * 数据库配置信息

     */

    private $config = null;

      

    /**

     * 数据库连接资源句柄

     */

    public $link = null;

      

    /**

     * 最近一次查询资源句柄

     */

    public $lastqueryid = null;

      

    /**

     *  统计数据库查询次数

     */

    public $querycount = 0;

      

    public function __construct() {

  

    }

      

    /**

     * 打开数据库连接,有可能不真实连接数据库

     * @param $config   数据库连接参数

     *        

     * @return void

     */

    public function open($config) {

        $this->config = $config;

        if($config[&#39;autoconnect&#39;] == 1) {

            $this->connect();

        }

    }

  

    /**

     * 真正开启数据库连接

     *        

     * @return void

     */

    public function connect() {

        //var_dump($this->config);

        if(!$this->link = @new mysqli($this->config[&#39;hostname&#39;], $this->config[&#39;username&#39;],$this->config[&#39;password&#39;], $this->config[&#39;database&#39;], $this->config[&#39;dbport&#39;])){

            $this->halt(&#39;Can not connect to MySQL server&#39;);

            return false;

        }

        if ($this->link->connect_errno) {

            $this->halt("Connect failed: %s\n", $this->link->connect_error);

            exit();

        }

  

        switch ($this->config[&#39;charset&#39;]){

            case &#39;utf8&#39;:

                $query_string = "

                         SET CHARACTER_SET_CLIENT = utf8,

                         CHARACTER_SET_CONNECTION = utf8,

                         CHARACTER_SET_DATABASE = utf8,

                         CHARACTER_SET_RESULTS = utf8,

                         CHARACTER_SET_SERVER = utf8,

                         COLLATION_CONNECTION = utf8_general_ci,

                         COLLATION_DATABASE = utf8_general_ci,

                         COLLATION_SERVER = utf8_general_ci,

                         sql_mode=&#39;&#39;";

                break;

            case &#39;GBK&#39;:

                $query_string = "

                        SET CHARACTER_SET_CLIENT = gbk,

                         CHARACTER_SET_CONNECTION = gbk,

                         CHARACTER_SET_DATABASE = gbk,

                         CHARACTER_SET_RESULTS = gbk,

                         CHARACTER_SET_SERVER = gbk,

                         COLLATION_CONNECTION = gbk_chinese_ci,

                         COLLATION_DATABASE = gbk_chinese_ci,

                         COLLATION_SERVER = gbk_chinese_ci,

                         sql_mode=&#39;&#39;";

                break;

            default:

                $error = "Db Error: charset is Invalid";

                $this->halt($error);

        }

        //进行编码声明

        if (!$this->link->query($query_string)){

            $this->halt("Db Error: ".mysqli_error($this->link));

        }

  

  

        $this->database = $this->config[&#39;database&#39;];

        return $this->link;

    }

  

    /**

     * 数据库查询执行方法

     * @param $sql 要执行的sql语句

     * @return 查询资源句柄

     */

    private function execute($sql) {

        //echo "<br>".$sql;

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

            $this->connect();

        }

          

        $this->lastqueryid = $this->link->query($sql);

        // $this->halt(mysqli_error($this->link), $sql);

  

        $this->querycount++;

        return $this->lastqueryid;

    }

  

    /**

     * 执行sql查询

     * @param $data         需要查询的字段值[例`name`,`gender`,`birthday`]

     * @param $table        数据表

     * @param $where        查询条件[例`name`=&#39;$name&#39;]

     * @param $limit        返回结果范围[例:10或10,10 默认为空]

     * @param $order        排序方式    [默认按数据库默认方式排序]

     * @param $group        分组方式    [默认为空]

     * @param $key          返回数组按键名排序

     * @return array        查询结果集数组

     */

    public function select($data, $table, $where = &#39;&#39;, $limit = &#39;&#39;, $order = &#39;&#39;, $group =&#39;&#39;, $key = &#39;&#39;) {

        $where = $where == &#39;&#39; ? &#39;&#39; : &#39; WHERE &#39;.$where;

        $order = $order == &#39;&#39; ? &#39;&#39; : &#39; ORDER BY &#39;.$order;

        $group = $group == &#39;&#39; ? &#39;&#39; : &#39; GROUP BY &#39;.$group;

        $limit = $limit == &#39;&#39; ? &#39;&#39; : &#39; LIMIT &#39;.$limit;

        $field = explode(&#39;,&#39;, $data);

        array_walk($field, array($this, &#39;add_special_char&#39;));

        $data = implode(&#39;,&#39;, $field);

  

        $sql = &#39;SELECT &#39;.$data.&#39; FROM `&#39;.$this->config[&#39;database&#39;].&#39;`.`&#39;.$table.&#39;`&#39;.$where.$group.$order.$limit;

        $this->execute($sql);

        if(!$this->lastqueryid) {

            return $this->lastqueryid;

        }

  

        $datalist = array();

        while(($rs = $this->fetch_next()) != false) {

            if($key) {

                $datalist[$rs[$key]] = $rs;

            } else {

                $datalist[] = $rs;

            }

        }

        $this->free_result();

        return $datalist;

    }

  

    /**

     * 获取单条记录查询

     * @param $data         需要查询的字段值[例`name`,`gender`,`birthday`]

     * @param $table        数据表

     * @param $where        查询条件

     * @param $order        排序方式    [默认按数据库默认方式排序]

     * @param $group        分组方式    [默认为空]

     * @return array/null   数据查询结果集,如果不存在,则返回空

     */

    public function get_one($data, $table, $where = &#39;&#39;, $order = &#39;&#39;, $group = &#39;&#39;) {

        $where = $where == &#39;&#39; ? &#39;&#39; : &#39; WHERE &#39;.$where;

        $order = $order == &#39;&#39; ? &#39;&#39; : &#39; ORDER BY &#39;.$order;

        $group = $group == &#39;&#39; ? &#39;&#39; : &#39; GROUP BY &#39;.$group;

        $limit = &#39; LIMIT 1&#39;;

        $field = explode( &#39;,&#39;, $data);

        array_walk($field, array($this, &#39;add_special_char&#39;));

        $data = implode(&#39;,&#39;, $field);

  

        $sql = &#39;SELECT &#39;.$data.&#39; FROM `&#39;.$this->config[&#39;database&#39;].&#39;`.`&#39;.$table.&#39;`&#39;.$where.$group.$order.$limit;

        $this->execute($sql);

        $res = $this->fetch_next();

        $this->free_result();

        return $res;

    }

      

    /**

     * 遍历查询结果集

     * @param $type     返回结果集类型

     *                  MYSQLI_ASSOC,MYSQL_NUM 和 MYSQL_BOTH

     * @return array

     */

    public function fetch_next($type=MYSQLI_ASSOC) {

        $res = mysqli_fetch_array($this->lastqueryid, $type);

        if(!$res) {

            $this->free_result();

        }

        return $res;

    }

      

    /**

     * 释放查询资源

     * @return void

     */

    public function free_result() {

        if(is_resource($this->lastqueryid)) {

            mysqli_free_result($this->lastqueryid);

            $this->lastqueryid = null;

        }

    }

      

    /**

     * 直接执行sql查询

     * @param $sql                          查询sql语句

     * @return  boolean/query resource      如果为查询语句,返回资源句柄,否则返回true/false

     */

    public function query($sql) {

        //echo "<Br>".$sql;

        return $this->execute($sql);

    }

      

    /**

     * 执行添加记录操作

     * @param $data         要增加的数据,参数为数组。数组key为字段值,数组值为数据取值

     * @param $table        数据表

     * @return boolean

     */

    public function insert($data, $table, $return_insert_id = false, $replace = false) {

        if(!is_array( $data ) || $table == &#39;&#39; || count($data) == 0) {

            return false;

        }

          

        $fielddata = array_keys($data);

        $valuedata = array_values($data);

        array_walk($fielddata, array($this, &#39;add_special_char&#39;));

        array_walk($valuedata, array($this, &#39;escape_string&#39;));

          

        $field = implode (&#39;,&#39;, $fielddata);

        $value = implode (&#39;,&#39;, $valuedata);

  

        $cmd = $replace ? &#39;REPLACE INTO&#39; : &#39;INSERT INTO&#39;;

        $sql = $cmd.&#39; `&#39;.$this->config[&#39;database&#39;].&#39;`.`&#39;.$table.&#39;`(&#39;.$field.&#39;) VALUES (&#39;.$value.&#39;)&#39;;

        $return = $this->execute($sql);

        return $return_insert_id ? $this->insert_id() : $return;

    }

      

    /**

     * 获取最后一次添加记录的主键号

     * @return int

     */

    public function insert_id() {

        return mysqli_insert_id($this->link);

    }

      

    /**

     * 执行更新记录操作

     * @param $data         要更新的数据内容,参数可以为数组也可以为字符串,建议数组。

     *                      为数组时数组key为字段值,数组值为数据取值

     *                      为字符串时[例:`name`=&#39;phpcms&#39;,`hits`=`hits`+1]。

     *                      为数组时[例: array(&#39;name&#39;=>&#39;phpcms&#39;,&#39;password&#39;=>&#39;123456&#39;)]

     *                      数组可使用array(&#39;name&#39;=>&#39;+=1&#39;, &#39;base&#39;=>&#39;-=1&#39;);程序会自动解析为`name` = `name` + 1, `base` = `base` - 1

     * @param $table        数据表

     * @param $where        更新数据时的条件

     * @return boolean

     */

    public function update($data, $table, $where = &#39;&#39;) {

        if($table == &#39;&#39; or $where == &#39;&#39;) {

            return false;

        }

  

        $where = &#39; WHERE &#39;.$where;

        $field = &#39;&#39;;

        if(is_string($data) && $data != &#39;&#39;) {

            $field = $data;

        } elseif (is_array($data) && count($data) > 0) {

            $fields = array();

            foreach($data as $k=>$v) {

                switch (substr($v, 0, 2)) {

                    case &#39;+=&#39;:

                        $v = substr($v,2);

                        if (is_numeric($v)) {

                            $fields[] = $this->add_special_char($k).&#39;=&#39;.$this->add_special_char($k).&#39;+&#39;.$this->escape_string($v, &#39;&#39;, false);

                        } else {

                            continue;

                        }

                          

                        break;

                    case &#39;-=&#39;:

                        $v = substr($v,2);

                        if (is_numeric($v)) {

                            $fields[] = $this->add_special_char($k).&#39;=&#39;.$this->add_special_char($k).&#39;-&#39;.$this->escape_string($v, &#39;&#39;, false);

                        } else {

                            continue;

                        }

                        break;

                    default:

                        $fields[] = $this->add_special_char($k).&#39;=&#39;.$this->escape_string($v);

                }

            }

            $field = implode(&#39;,&#39;, $fields);

        } else {

            return false;

        }

  

        $sql = &#39;UPDATE `&#39;.$this->config[&#39;database&#39;].&#39;`.`&#39;.$table.&#39;` SET &#39;.$field.$where;

        return $this->execute($sql);

    }

      

    /**

     * 执行删除记录操作

     * @param $table        数据表

     * @param $where        删除数据条件,不充许为空。

     *                      如果要清空表,使用empty方法

     * @return boolean

     */

    public function delete($table, $where) {

        if ($table == &#39;&#39; || $where == &#39;&#39;) {

            return false;

        }

        $where = &#39; WHERE &#39;.$where;

        $sql = &#39;DELETE FROM `&#39;.$this->config[&#39;database&#39;].&#39;`.`&#39;.$table.&#39;`&#39;.$where;

        return $this->execute($sql);

    }

      

    /**

     * 获取最后数据库操作影响到的条数

     * @return int

     */

    public function affected_rows() {

        return mysql_affected_rows($this->link);

    }

      

    /**

     * 获取数据表主键

     * @param $table        数据表

     * @return array

     */

    public function get_primary($table) {

        $this->execute("SHOW COLUMNS FROM $table");

        while($r = $this->fetch_next()) {

            if($r[&#39;Key&#39;] == &#39;PRI&#39;) break;

        }

        return $r[&#39;Field&#39;];

    }

  

    /**

     * 获取表字段

     * @param $table        数据表

     * @return array

     */

    public function get_fields($table) {

        $fields = array();

        $this->execute("SHOW COLUMNS FROM $table");

        while($r = $this->fetch_next()) {

            $fields[$r[&#39;Field&#39;]] = $r[&#39;Type&#39;];

        }

        return $fields;

    }

  

    /**

     * 检查不存在的字段

     * @param $table 表名

     * @return array

     */

    public function check_fields($table, $array) {

        $fields = $this->get_fields($table);

        $nofields = array();

        foreach($array as $v) {

            if(!array_key_exists($v, $fields)) {

                $nofields[] = $v;

            }

        }

        return $nofields;

    }

  

    /**

     * 检查表是否存在

     * @param $table 表名

     * @return boolean

     */

    public function table_exists($table) {

        $tables = $this->list_tables();

        return in_array($table, $tables) ? 1 : 0;

    }

      

    public function list_tables() {

        $tables = array();

        $this->execute("SHOW TABLES");

        while($r = $this->fetch_next()) {

            $tables[] = $r[&#39;Tables_in_&#39;.$this->config[&#39;database&#39;]];

        }

        return $tables;

    }

  

    /**

     * 检查字段是否存在

     * @param $table 表名

     * @return boolean

     */

    public function field_exists($table, $field) {

        $fields = $this->get_fields($table);

        return array_key_exists($field, $fields);

    }

  

    public function num_rows($sql) {

        $this->lastqueryid = $this->execute($sql);

        return mysqli_num_rows($this->lastqueryid);

    }

  

    public function num_fields($sql) {

        $this->lastqueryid = $this->execute($sql);

        return mysqli_num_fields($this->lastqueryid);

    }

  

    public function result($sql, $row) {

        $this->lastqueryid = $this->execute($sql);

        return @mysql_result($this->lastqueryid, $row);

    }

  

    public function error() {

        return @mysqli_error($this->link);

    }

  

    public function errno() {

        return intval(@mysqli_errno($this->link)) ;

    }

  

    public function version() {

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

            $this->connect();

        }

        return mysqli_get_server_info($this->link);

    }

  

    public function close() {

        if (is_resource($this->link)) {

            @mysqli_close($this->link);

        }

    }

      

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

        if($this->config[&#39;debug&#39;]) {

            $this->errormsg = "<b>MySQL Query : </b> $sql <br /><b> MySQL Error : </b>".$this->error()." <br /> <b>MySQL Errno : </b>".$this->errno()." <br /><b> Message : </b> $message <br /><a href=&#39;http://faq.phpcms.cn/?errno=".$this->errno()."&msg=".urlencode($this->error())."&#39; target=&#39;_blank&#39; style=&#39;color:red&#39;>Need Help?</a>";

            $msg = $this->errormsg;

            echo &#39;<div style="font-size:12px;text-align:left; border:1px solid #9cc9e0; padding:1px 4px;color:#000000;font-family:Arial, Helvetica,sans-serif;"><span>&#39;.$msg.&#39;</span></div>&#39;;

            exit;

        } else {

            return false;

        }

    }

  

    /**

     * 对字段两边加反引号,以保证数据库安全

     * @param $value 数组值

     */

    public function add_special_char(&$value) {

        if(&#39;*&#39; == $value || false !== strpos($value, &#39;(&#39;) || false !== strpos($value, &#39;.&#39;) || false !== strpos ( $value, &#39;`&#39;)) {

            //不处理包含* 或者 使用了sql方法。

        } else {

            $value = &#39;`&#39;.trim($value).&#39;`&#39;;

        }

        if (preg_match("/\b(select|insert|update|delete)\b/i", $value)) {

            $value = preg_replace("/\b(select|insert|update|delete)\b/i", &#39;&#39;, $value);

        }

        return $value;

    }

      

    /**

     * 对字段值两边加引号,以保证数据库安全

     * @param $value 数组值

     * @param $key 数组key

     * @param $quotation

     */

    public function escape_string(&$value, $key=&#39;&#39;, $quotation = 1) {

        if ($quotation) {

            $q = &#39;\&#39;&#39;;

        } else {

            $q = &#39;&#39;;

        }

        $value = $q.$value.$q;

        return $value;

    }

}

// here&#39;s a rough replacement using mysqli:

// 错略的使用mysqli替换

if(!function_exists(&#39;mysql_result&#39;)) {

    function mysql_result($result, $number, $field=0) {

        mysqli_data_seek($result, $number);

        $row = mysqli_fetch_array($result);

        return $row[$field];

    }

}

?>

ログイン後にコピー

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)