通过php pdo链接MYSQL数据库,进行简单的数据操作,我的网站[爱玩电脑 aowana.sinaapp.com],目前使用的就是这个类,如果该类更新会继续分享到oschina。
1. [代码]非常简单的PHP通过PDO操作MYSQL类 mysql.class.php
<?php /** * MySQL操作类 * http://www.php.cn/ * 日期:2015-04-01 */ class mysql { public $pdo = null; public $results = null; public function find($sql, $array=array()) { $ok = $this->process($sql, $array); if ($ok) { $this->results->setFetchMode(PDO::FETCH_ASSOC); $data = $this->results->fetch(); return $data; } else { return false; } } public function finds($sql, $array=array()) { $ok = $this->process($sql, $array); if ($ok) { $this->results->setFetchMode(PDO::FETCH_ASSOC); $data = $this->results->fetchAll(); return $data; } else { return array(); } } public function update($sql, $array=array()) { $ok = $this->process($sql, $array); if ($ok === false) return -1;//执行出错返回-1 else if ($ok) return $this->results->rowCount(); else return 0; } public function insert($sql, $array=array()) { $ok = $this->process($sql, $array); if ($ok) { $id = $this->pdo->lastInsertId(); $id = $id ? $id : 1; return $id; } else { return false; } } public function delete($sql, $array=array()) { $ok = $this->process($sql, $array); if ($ok === false) return -1;//执行出错返回-1 else if ($ok) return $this->results->rowCount(); else return 0; } public function query($sql, $array=array()) { return $this->process($sql, $array); } private function process($sql, $array) { if (is_null($this->pdo)) $this->connect(); $this->results = $this->pdo->prepare($sql); //print_r($this->pdo->errorInfo()); //print_r($this->results->errorInfo()); return $this->results->execute($array); } private function connect() { try { $this->pdo = new PDO('mysql:host=' . MYSQL_HOST . ';port=' . MYSQL_PORT . ';dbname=' . MYSQL_DATABASE . ';charset=utf8', MYSQL_USERNAME, MYSQL_PASSWORD); } catch (PDOException $error) { $html = $error->getMessage(); //SAE Mail to Master http503(); } } }
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器