首頁 > 後端開發 > php教程 > PHP基於pdo的資料庫操作類別【可支援mysql、sqlserver及oracle】

PHP基於pdo的資料庫操作類別【可支援mysql、sqlserver及oracle】

不言
發布: 2023-03-28 22:34:02
原創
1978 人瀏覽過

這篇文章主要介紹了PHP基於pdo的資料庫操作類別,可實現基本的資料庫連線、增刪改查、關閉連線等操作,也支援針對mysql、sqlserver及oracle等資料庫的操作,需要的朋友可以參考下

本文實例講述了PHP基於pdo的資料庫操作類別。分享給大家供大家參考,具體如下:

工作中需要操作sqlserver、oracle都是使用的這個類,當時是在別人的基礎上改進了,現在分享下

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

<?php

class Pdodb{

  protected $pdo;

  protected $res;

  protected $config;

  /*构造函数*/

  function __construct($config){

    $this->Config = $config;

    $this->connect();

  }

  /*数据库连接*/

  public function connect(){

    try {

       $this->pdo= new PDO($this->Config[&#39;dsn&#39;], $this->Config[&#39;username&#39;], $this->Config[&#39;password&#39;]);//$dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $user, $pass);

       $this->pdo->query("set names utf8");

    }catch(Exception $e){

      echo &#39;数据库连接失败,详情: &#39; . $e->getMessage () . &#39; 请在配置文件中数据库连接信息&#39;;

      exit ();

    }

    /*

    if($this->Config[&#39;type&#39;]==&#39;oracle&#39;){

      $this->pdo->query("set names {$this->Config[&#39;charset&#39;]};");

    }else{

      $this->pdo->query("set names {$this->Config[&#39;charset&#39;]};");

    }

    */

    //把结果序列化成stdClass

    //$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    //自己写代码捕获Exception

    //$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);//属性名 属性值 数组以关联数组返回

  }

  /*数据库关闭*/

  public function close(){

    $this->pdo = null;

  }

  //用于有记录结果返回的操作,特别是SELECT操作

  public function query($sql,$return=false){

    $res = $this->pdo->query($sql);

    if($res){

      $this->res = $res; // 未返回 return $this->res;

    }

    if($return){

      return $res;

    }

  }

  //主要是针对没有结果集合返回的操作,比如INSERT、UPDATE、DELETE等操作

  public function exec($sql,$return=false){

    $res = $this->pdo->exec($sql);

    if($res){

      $this->res = $res;

    }

    if($return){//返回操作是否成功 成功返回1 失败0

      return $res;

    }

  }

  //将$this->res以数组返回(全部返回)

  public function fetchAll(){

    return $this->res->fetchAll();

  }

  //将$this->res以数组返回(一条记录)

  public function fetch(){

    return $this->res->fetch();

  }

  //返回所有字段

  public function fetchColumn(){

    return $this->res->fetchColumn();

  }

  //返回最后插入的id

  public function lastInsertId(){

    return $this->res->lastInsertId();

  }

  //返回最后插入的id

  public function lastInsertId2(){

    return $this->pdo->lastInsertId();

  }

  /**

  * 参数说明

  * string/array $table 数据库表,两种传值模式

  * 普通模式:

  * &#39;tb_member, tb_money&#39;

  * 数组模式:

  * array(&#39;tb_member&#39;, &#39;tb_money&#39;)

  * string/array $fields 需要查询的数据库字段,允许为空,默认为查找全部,两种传值模式

  * 普通模式:

  * &#39;username, password&#39;

  * 数组模式:

  * array(&#39;username&#39;, &#39;password&#39;)

  * string/array $sqlwhere 查询条件,允许为空,两种传值模式

  * 普通模式(必须加上and,$sqlwhere为空 1=1 正常查询):

  * &#39;and type = 1 and username like "%os%"&#39;

  * 数组模式:

  * array(&#39;type = 1&#39;, &#39;username like "%os%"&#39;)

  * string $orderby 排序,默认为id倒序

  *int $debug 是否开启调试,开启则输出sql语句

  * 0 不开启

  * 1 开启

  * 2 开启并终止程序

  * int $mode 返回类型

  * 0 返回多条记录

  * 1 返回单条记录

  * 2 返回行数

  */

  public function select($table, $fields="*", $sqlwhere="", $orderby="", $debug=0, $mode=0){

    //参数处理

    if(is_array($table)){

      $table = implode(&#39;, &#39;, $table);

    }

    if(is_array($fields)){

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

      /*

      if($this->Config[&#39;type&#39;]==&#39;oracle&#39;){

        //$fields = implode(&#39;,&#39;,$fields);//CUSTOMER_ID,FIRST_NAME,LAST_NAME,EMAIL

        //$fields = implode(",&#39;UTF8&#39;,&#39;ZHS16GBK&#39;) ,convert(",$fields);

        //$fields="convert(".$fields.",&#39;UTF8&#39;,&#39;ZHS16GBK&#39;)";

      }else{

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

      }

      */

    }

    if(is_array($sqlwhere)){

      $sqlwhere = &#39; and &#39;.implode(&#39; and &#39;, $sqlwhere);

    }

    //数据库操作

    if($debug === 0){

      if($mode === 2){ //统计

        $this->query("select count(*) from $table where 1=1 $sqlwhere");

        $return = $this->fetchColumn();

      }else if($mode === 1){ //返回一条

        $this->query("select $fields from $table where 1=1 $sqlwhere $orderby");

        $return = $this->fetch();

      }else{

        $this->query("select $fields from $table where 1=1 $sqlwhere $orderby");

        $return = $this->fetchAll();//如果 $this->res为空即sql语句错误 会提示Call to a member function fetchAll() on a non-object

      }

      return $return;

    }else{

        if($mode === 2){

          echo "select count(*) from $table where 1=1 $sqlwhere";

        }else if($mode === 1){

          echo "select $fields from $table where 1=1 $sqlwhere $orderby";

        }else{

          echo "select $fields from $table where 1=1 $sqlwhere $orderby";

        }

        if($debug === 2){

          exit;

        }

    }

  }

  /**

  * 参数说明

  * string/array $table 数据库表,两种传值模式

  * 普通模式:

  * &#39;tb_member, tb_money&#39;

  * 数组模式:

  * array(&#39;tb_member&#39;, &#39;tb_money&#39;)

  * string/array $set 需要插入的字段及内容,两种传值模式

  * 普通模式:

  * &#39;username = "test", type = 1, dt = now()&#39;

  * 数组模式:

  * array(&#39;username = "test"&#39;, &#39;type = 1&#39;, &#39;dt = now()&#39;)

  * int $debug 是否开启调试,开启则输出sql语句

  * 0 不开启

  * 1 开启

  * 2 开启并终止程序

  * int $mode 返回类型

  * 0 无返回信息

  * 1 返回执行条目数

  * 2 返回最后一次插入记录的id

  */

  public function oic_insert($table, $set, $debug=0, $mode=0){

    //参数处理

    if(is_array($table)){

      $table = implode(&#39;, &#39;, $table);

    }

    if(is_array($set)){

      $s=&#39;&#39;;$i=0;

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

        $i++;

        $s[$i]=$k;//,连接

        $val[$i]=$v;

      }

      $sarr=implode(",",$s);//去掉最后一个,

      //array_pop($sarr);

      $set=implode("&#39;,&#39;",$val);////15221579236&#39;,&#39;张三&#39;,&#39;&#39;,&#39;2001&#39;,&#39;8&#39;,&#39;4&#39;,&#39;女&#39;,&#39;是

      //$set = implode(&#39;, &#39;, $set);

    }

    //数据库操作

    if($debug === 0){

      if($mode === 2){

        $this->query("insert into $table ($sarr) values(&#39;".$set."&#39;)");

        //$return = $this->lastInsertId();

      }else if($mode === 1){

        $this->exec("insert into $table ($sarr) values(&#39;".$set."&#39;)");

        $return = $this->res;

      }else{

        $this->query("insert into $table ($sarr) values(&#39;".$set."&#39;)");

        $return = NULL;

      }

      return $return;

    }else{

      echo "insert into $table ($sarr) values(&#39;".$set."&#39;)";

      if($debug === 2){

        exit;

      }

    }

  }

  public function insert($table, $set, $debug=0, $mode=0){

    //参数处理

    if(is_array($table)){

      $table = implode(&#39;, &#39;, $table);

    }

    if(is_array($set)){

      $s=&#39;&#39;;

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

        $s.=$k."=&#39;".$v."&#39;,";//,连接

      }

      $sarr=explode(&#39;,&#39;,$s);//去掉最后一个,

      array_pop($sarr);

      $set=implode(&#39;,&#39;,$sarr);

      //$set = implode(&#39;, &#39;, $set);

    }

    //数据库操作

    if($debug === 0){

      if($mode === 2){

        $this->query("insert into $table set $set");

        $return = $this->pdo->lastInsertId();

      }else if($mode === 1){

        $this->exec("insert into $table set $set");

        $return = $this->res;

      }else{

        $this->query("insert into $table set $set");

        $return = NULL;

      }

      return $return;

    }else{

      echo "insert into $table set $set";

      if($debug === 2){

        exit;

      }

    }

  }

  /**

  * 参数说明

  * string $table 数据库表,两种传值模式

  * 普通模式:

  * &#39;tb_member, tb_money&#39;

  * 数组模式:

  * array(&#39;tb_member&#39;, &#39;tb_money&#39;)

  * string/array $set 需要更新的字段及内容,两种传值模式

  * 普通模式:

  * &#39;username = "test", type = 1, dt = now()&#39;

  * 数组模式:

  * array(&#39;username = "test"&#39;, &#39;type = 1&#39;, &#39;dt = now()&#39;)

  * string/array $sqlwhere 修改条件,允许为空,两种传值模式

  * 普通模式:

  * &#39;and type = 1 and username like "%os%"&#39;

  * 数组模式:

  * array(&#39;type = 1&#39;, &#39;username like "%os%"&#39;)

  * int $debug 是否开启调试,开启则输出sql语句

  * 0 不开启

  * 1 开启

  * 2 开启并终止程序

  * int $mode 返回类型

  * 0 无返回信息

  * 1 返回执行条目数

  */

  public function update($table, $set, $sqlwhere="", $debug=0, $mode=0){

    //参数处理

    if(is_array($table)){

      $table = implode(&#39;, &#39;, $table);

    }

    if(is_array($set)){

      $s=&#39;&#39;;

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

        $s.=$k."=&#39;".$v."&#39;,";

      }

      $sarr=explode(&#39;,&#39;,$s);//去掉最后一个,

      array_pop($sarr);

      $set=implode(&#39;,&#39;,$sarr);

      //$set = implode(&#39;, &#39;, $set);

    }

    if(is_array($sqlwhere)){

      $sqlwhere = &#39; and &#39;.implode(&#39; and &#39;, $sqlwhere);

    }

    //数据库操作

    if($debug === 0){

      if($mode === 1){

        $this->exec("update $table set $set where 1=1 $sqlwhere");

        $return = $this->res;

      }else{

        $this->query("update $table set $set where 1=1 $sqlwhere");

        $return = true;

      }

      return $return;

    }else{

      echo "update $table set $set where 1=1 $sqlwhere";

      if($debug === 2){

        exit;

      }

    }

  }

  /**

  * 参数说明

  * string $table 数据库表

  * string/array $sqlwhere 删除条件,允许为空,两种传值模式

  * 普通模式:

  * &#39;and type = 1 and username like "%os%"&#39;

  * 数组模式:

  * array(&#39;type = 1&#39;, &#39;username like "%os%"&#39;)

  * int $debug 是否开启调试,开启则输出sql语句

  * 0 不开启

  * 1 开启

  * 2 开启并终止程序

  * int $mode 返回类型

  * 0 无返回信息

  * 1 返回执行条目数

  */

  public function delete($table, $sqlwhere="", $debug=0, $mode=0){

    //参数处理

    if(is_array($sqlwhere)){

      $sqlwhere = &#39; and &#39;.implode(&#39; and &#39;, $sqlwhere); //是字符串需自己加上and

    }

    //数据库操作

    if($debug === 0){

      if($mode === 1){

        $this->exec("delete from $table where 1=1 $sqlwhere");

        $return = $this->res;

      }else{

        $this->query("delete from $table where 1=1 $sqlwhere");

        $return = NULL;

      }

      return $return;

    }else{

      echo "delete from $table where 1=1 $sqlwhere";

      if($debug === 2){

        exit;

      }

    }

  }

}

/*

sqlserver 配置 extension=php_pdo_mssql.dll和extension=php_pdo_sqlsrv.dll 安装对应的 ntwdblib.dll

http://msdn.microsoft.com/en-us/library/cc296170.aspx 下载php版本对应的sqlsrv扩展

sqlserver 配置 odbc连接需开启extension=php_pdo_odbc.dll

*/

$mssql2008_config=array(

  &#39;dsn&#39;=>&#39;odbc:Driver={SQL Server};Server=192.168.1.60;Database=his&#39;,//数据库服务器地址

  &#39;username&#39;=>&#39;sa&#39;,

  &#39;password&#39;=>&#39;xxxxx&#39;,

);

$mssql=new Pdodb($mssql2008_config);

$sql="select * from

(

  select row_number()over(order by tempcolumn)temprownumber,*

    from (

      select top 10 tempcolumn=0,a.*

      from DA_GR_HBFS a

      where 1=1

    ) t

) tt

where temprownumber>0";

$mssql->query($sql);

while($res=$mssql->fetch()){

  $data[]=$res;

}

print_r($data);exit;

//mysql 操作

$msyql_config=array(

  &#39;dsn&#39;=>&#39;mysql:host=localhost;dbname=talk&#39;,

  &#39;username&#39;=>&#39;root&#39;,

  &#39;password&#39;=>&#39;123456&#39;

);

$mysql=new PDO_DB($msyql_config);

$sql = &#39;SELECT user_id, user_name, nickname FROM et_users &#39;;

$mysql->query($sql);

$data=$mysql->fetchAll();

print_r($data);exit;

//oracle 操作

$oci_config=array(

  &#39;dsn&#39;=>&#39;oci:dbname=orcl&#39;,

  &#39;username&#39;=>&#39;BAOCRM&#39;,

  &#39;password&#39;=>&#39;BAOCRM&#39;

);

$oracle=new PDO_DB($oci_config);

//print_r($oracle);exit;//PDO_DB Object ( [pdo:protected] => PDO Object ( ) [res:protected] => [config:protected] => [Config] => Array ( [dsn] => oci:dbname=orcl [name] => PWACRM [password] => PWACRM ) )

$sql="select * from CUSTOMER_LEVEL t";

$oracle->query($sql);

$data=$oracle->fetchAll();

print_r($data);exit;

/*

Array

(

  [0] => Array

    (

      [LEVEL_ID] => 1

      [0] => 1

      [LEVEL_NAME] => 普通会员

      [1] => 普通会员

      [LEVEL_DETAIL] => 普通会员

      [2] => 普通会员

      [SORT_NUMBER] => 15

      [3] => 15

      [CREATE_TIME] => 12-7月 -12

      [4] => 12-7月 -12

      [CREATE_BY] => 1

      [5] => 1

      [UPDATE_TIME] => 12-7月 -12

      [6] => 12-7月 -12

      [UPDATE_BY] => 1

      [7] => 1

      [STATE] => 正常

      [8] => 正常

    )

)*/

?>

登入後複製

相關推薦:

PHP實作的mysql讀寫分離運算

PHP實作可防止表單重複提交功能(基於token驗證)

以上是PHP基於pdo的資料庫操作類別【可支援mysql、sqlserver及oracle】的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板