我的全部文章整理在此篇,將陸續更新收錄:知無涯,行者之路莫言終(個人編程之路)php
PHP Data Object 數據庫訪問抽象層 統一各類數據庫訪問接口
複製代碼
調用一個函數便可輸出一個界面。默認PDO是支持MySQL的html
<?php
phpinfo();
複製代碼
若是不支持,在php.ini中打開選項便可前端
|-- ---------------
$dsn = 'mysql:host=localhost;dbname=datatype';//數據源
$user = 'root';
$pwd = 'xxxxx';
$conn = new PDO($dsn, $user, $pwd);
var_dump($conn);//object(PDO)#1 (0) { }
複製代碼
---->[pdo/pdo_conn.php]------------------
$path = __DIR__.'\config.txt';
$dsn = 'uri:file://' . $path . '';//數據源
$user = 'root';
$pwd = 'xxxxx';
$conn = new PDO($dsn, $user, $pwd);
var_dump($conn);//object(PDO)#1 (0) { }
---->[pdo/config.txt]------------------
mysql:dbname=datatype;host=localhost
複製代碼
exec()
建立表不支持查詢操做,返回受影響的行數。數據表使用此文中的pic表:MySQL指南之SQL語句基礎mysql
try {
$dsn = 'mysql:host=localhost;dbname=datatype';//數據源
$user = 'root';
$pwd = 'toly';
$conn = new PDO($dsn, $user, $pwd);
//---------------------建表--------------------------
$sql_create_table = <<<EOT
CREATE TABLE IF NOT EXISTS php_pic(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
pic_path VARCHAR(120) NOT NULL,
pic_length INT UNSIGNED DEFAULT 0,
pic_mime TINYINT UNSIGNED,
pic_width SMALLINT UNSIGNED,
pic_height SMALLINT UNSIGNED
);
EOT;
$len = $conn->exec($sql_create_table);
echo $len;//0
} catch (Exception $e) {
$e->getMessage();
}
mysql> SHOW TABLES;
+--------------------+
| Tables_in_datatype |
+--------------------+
| php_pic |
+--------------------+
mysql> DESC php_pic;
+------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| pic_path | varchar(120) | NO | | NULL | |
| pic_length | int(10) unsigned | YES | | 0 | |
| pic_mime | tinyint(3) unsigned | YES | | NULL | |
| pic_width | smallint(5) unsigned | YES | | NULL | |
| pic_height | smallint(5) unsigned | YES | | NULL | |
+------------+----------------------+------+-----+---------+----------------+
複製代碼
//---------------------插入記錄--------------------------
$sql_insert = <<<EOT
INSERT INTO pic(pic_path,pic_length,pic_mime,pic_width,pic_height) VALUES
('30000X20000.jpg',116342886,1,30000,20000),
('3000X2000.jpg',3404969,1,3000,2000),
('300X200.jpg',99097,1,300,200),
('30X20.jpg',10158,1,30,20),
('6dc9e8455c47d964e1a8a4ef04cf9477.jpg',236254,1,974,319);
EOT;
$len = $conn->exec($sql_insert);
echo $len;//5
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 1 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 2 | 3000X2000.jpg | 3404969 | 1 | 3000 | 2000 |
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 974 | 319 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
//---------------------修改記錄--------------------------
$sql_update = <<<EOT
UPDATE php_pic SET pic_height=10086,pic_width=2333
WHERE id =5;
EOT;
$len = $conn->exec($sql_update);//1
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 1 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 2 | 3000X2000.jpg | 3404969 | 1 | 3000 | 2000 |
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
//---------------------刪除記錄--------------------------
$sql_delete = <<<EOT
DELETE FROM php_pic
WHERE pic_width> 2500;
EOT;
$len = $conn->exec($sql_delete);//2
echo $len;
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
關於錯誤信息的獲取sql
$sql_delete = <<<EOT
DELETE FROM php_picXXX
WHERE pic_width> 2500;
EOT;
$len = $conn->exec($sql_delete);//2
if ($len === false) {
echo $conn->errorCode();
echo "<hr/>";
$err= $conn->errorInfo();
print_r($err);
}
---->[命令行]------------------
mysql> DELETE FROM php_picXXX
-> WHERE pic_width> 2500;
ERROR 1146 (42S02): Table 'datatype.php_picxxx' doesn't exist 複製代碼
返回一個PDOStatement 對象,能夠遍歷獲取數據數據庫
$sql_query = <<<EOT
SELECT * FROM php_pic;
EOT;
$res = $conn->query($sql_query);
foreach ($res as $data) {
print_r($data);
}
複製代碼
打印出記錄信息編程
$sql_query = <<<EOT
SELECT * FROM php_pic;
EOT;
$res = $conn->query($sql_query);
foreach ($res as $data) {
echo "id:" . $data["id"] . "<br/>";
echo "路徑: " . $data["pic_path"] . "<br/>";
echo "大小: " . $data["pic_length"] . "<br/>";
echo "類型: " . $data["pic_mime"] . "<br/>";
echo "圖片寬: " . $data["pic_width"] . "<br/>";
echo "圖片高: " . $data["pic_height"] . "<br/>";
echo "<hr/>";
}
複製代碼
$cursor = $conn->prepare($sql_query);//準備
$res = $cursor->execute();//執行
if ($res) {
while ($data = $cursor->fetch()) {
echo "id:" . $data["id"] . "<br/>";
echo "路徑: " . $data["pic_path"] . "<br/>";
echo "大小: " . $data["pic_length"] . "<br/>";
echo "類型: " . $data["pic_mime"] . "<br/>";
echo "圖片寬: " . $data["pic_width"] . "<br/>";
echo "圖片高: " . $data["pic_height"] . "<br/>";
echo "<hr/>";
}
}
複製代碼
其中fetch能夠傳入參數,來控制結果的形式,下面舉幾個小例子後端
$attr_arr = ['AUTOCOMMIT','ERRMODE','CASE','PERSISTENT','TIMEOUT','ORACLE_NULLS',
'SERVER_INFO','SERVER_VERSION', 'CONNECTION_STATUS',
];
foreach ($attr_arr as $attr) {
$attr="PDO::ATTR_$attr";
echo $attr . "----:";
$attr = constant($attr);
echo $conn->getAttribute($attr) . '<br/>';
}
//PDO::ATTR_AUTOCOMMIT----:1
//PDO::ATTR_ERRMODE----:0
//PDO::ATTR_CASE----:0
//PDO::ATTR_PERSISTENT----:
//PDO::ATTR_TIMEOUT----:
//Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in J:\PHP\toly\pdo\pdo_conn.php on line 88
//
//PDO::ATTR_ORACLE_NULLS----:0
//PDO::ATTR_SERVER_INFO----:Uptime: 187237 Threads: 2 Questions: 969 Slow queries: 0 Opens: 2033 Flush tables: 1 Open tables: 1004 Queries per second avg: 0.005
//PDO::ATTR_SERVER_VERSION----:5.7.22
//PDO::ATTR_CONNECTION_STATUS----:localhost via TCP/IP
$conn->setAttribute(鍵,值) # 設置屬性
複製代碼
---->[pdo/form.php]------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加頁面</title>
</head>
<body>
<h1>添加頁面</h1>
<form action="do_add_pic.php" method="post">
<label>圖片路徑:</label>
<input type="text" name="pic_path" placeholder="請輸入圖片路徑"><br>
<label>圖片大小:</label>
<input type="number" name="pic_length" placeholder="請輸入圖片大小"><br>
<label>圖片類型:</label>
<select id="select" name="pic_mime">
<option value="png">png</option>
<option value="jpg">jpg/jpeg</option>
</select><br>
<label>圖片寬:</label>
<input type="number" name=" pic_width" placeholder=" 圖片寬">
<label>圖片高:</label>
<input type="number" name=" pic_height" placeholder=" 圖片高"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
---->[pdo/do_add_pic.php]------------------------------
<?php
$pic_path = $_POST['pic_path'];
$pic_length = $_POST['pic_length'];
$pic_mime = $_POST['pic_mime'];
$pic_width = $_POST['pic_width'];
$pic_height = $_POST['pic_height'];
$pic_mime = $pic_mime === "png" ? 0 : 1;
echo $pic_path . '<br/>';
echo $pic_length . '<br/>';
echo $pic_mime . '<br/>';
echo $pic_width . '<br/>';
echo $pic_height . '<br/>';
複製代碼
$dsn = 'mysql:host=localhost;dbname=datatype';//數據源
$user = 'root';
$pwd = 'xxxxx';
$conn = new PDO($dsn, $user, $pwd);
$sql_insert = "INSERT INTO php_pic(pic_path,pic_length,pic_mime,pic_width,pic_height) VALUES ('$pic_path',$pic_length,$pic_mime,$pic_width,$pic_height);";
$exec = $conn->exec($sql_insert);//5
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
| 6 | hello.jpg | 88 | 1 | 99 | 99 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
---->[pdo/get_pic.php]------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查詢頁面</title>
</head>
<body>
<h1>查詢頁面</h1>
<form action="do_find_pic.php" method="post">
<label>圖片路徑:</label>
<input type="text" name="pic_path" placeholder="請輸入圖片路徑"><br>
<label>圖片大小:</label>
<input type="number" name="pic_length" placeholder="請輸入圖片大小"><br>
<input type="submit" name="獲取">
</form>
</body>
</html>
---->[pdo/do_find_pic.php]------------------------------
$pic_path = $_POST['pic_path'];
$pic_length = $_POST['pic_length'];
$dsn = 'mysql:host=localhost;dbname=datatype';//數據源
$user = 'root';
$pwd = 'toly';
$conn = new PDO($dsn, $user, $pwd);
$sql_query = <<<EOT
SELECT * FROM php_pic WHERE pic_path = '$pic_path' AND pic_length= $pic_length;
EOT;
$cursor = $conn->prepare($sql_query);//準備
$res = $cursor->execute();//執行
if ($res) {
$table = "<table border='1' cellspacing='0' cellpadding='0' width='70%' >"
$table .= "<tr/>";
$table .= "<td >id</td>";
$table .= "<td >pic_path</td>";
$table .= "<td >pic_length</td>";
$table .= "<td >pic_mime</td>";
$table .= "<td >pic_width</td>";
$table .= "<td >pic_height</td>";
$table .= "</tr>";
while ($data = $cursor->fetch()) {
$table .= "<tr/>";
$table .= "<td >" . $data["id"] . "</td>";
$table .= "<td >" . $data["pic_path"] . "</td>";
$table .= "<td >" . $data["pic_length"] . "</td>";
$table .= "<td >" . $data["pic_mime"] . "</td>";
$table .= "<td >" . $data["pic_width"] . "</td>";
$table .= "<td >" . $data["pic_height"] . "</td>";
$table .= "</tr>";
}
}
echo $table;
複製代碼
也就是用戶故意在表單裏寫入sql語句,致使應用的行爲異常,
解決方法很簡單,也就是將用戶的輸入都變成字符串,特殊符號轉義數組
echo $pic_path.'<br/>';//'or 1=1 # echo $conn->quote($pic_path);//'\'or 1=1 #'
$sql_query = <<<EOT
SELECT * FROM php_pic WHERE pic_path = $pic_path AND pic_length= $pic_length;
EOT;
複製代碼
$sql_query = <<<EOT
SELECT * FROM php_pic WHERE pic_path = :pic_path AND pic_length= :pic_length;
EOT;
$cursor = $conn->prepare($sql_query);//準備
$res = $cursor->execute([':pic_path'=>$pic_path,':pic_length'=>$pic_length]);//執行
複製代碼
接下來的另外一種佔位形式可謂他鄉遇故知啊,和Android一毛同樣bash
$sql_query = <<<EOT
SELECT * FROM php_pic WHERE pic_path =? AND pic_length=?;
EOT;
$cursor = $conn->prepare($sql_query);//準備
$res = $cursor->execute([$pic_path, $pic_length]);//執行
複製代碼
參數綁定到變量好處很明顯,變更起來方便
$sql_insert = "INSERT INTO php_pic(pic_path,pic_length,pic_mime,pic_width,pic_height) VALUES (:pic_path,:pic_length,:pic_mime,:pic_width,:pic_height);";
$state = $conn->prepare($sql_insert);
$state->bindParam(':pic_path', $pic_path, PDO::PARAM_STR);
$state->bindParam(':pic_length', $pic_length, PDO::PARAM_INT);
$state->bindParam(':pic_mime', $pic_mime, PDO::PARAM_INT);
$state->bindParam(':pic_width', $pic_width, PDO::PARAM_INT);
$state->bindParam(':pic_height', $pic_height, PDO::PARAM_INT);
$state->execute();
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
| 6 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 7 | 30000X20000.jpg | 116342886 | 1 | 30000 | 99 |
| 8 | 30000X20000.jpg | 116342886 | 1 | 99 | 99 |
| 9 | hello.jpg | 88 | 1 | 99 | 99 |
| 10 | card.png | 3333 | 0 | 4567 | 7889 |
+----+--------------------------------------+------------+----------+-----------+------------+
|--- 問號型的綁定
$sql_insert = "INSERT INTO php_pic(pic_path,pic_length,pic_mime,pic_width,pic_height) VALUES (?,?,?,?,?);";
$state = $conn->prepare($sql_insert);
$state->bindParam(1, $pic_path, PDO::PARAM_STR);
$state->bindParam(2, $pic_length, PDO::PARAM_INT);
$state->bindParam(3, $pic_mime, PDO::PARAM_INT);
$state->bindParam(4, $pic_width, PDO::PARAM_INT);
$state->bindParam(5, $pic_height, PDO::PARAM_INT);
$state->execute();
---->[命令行]------------------
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
| 6 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 7 | 30000X20000.jpg | 116342886 | 1 | 30000 | 99 |
| 8 | 30000X20000.jpg | 116342886 | 1 | 99 | 99 |
| 9 | hello.jpg | 88 | 1 | 99 | 99 |
| 10 | card.png | 3333 | 0 | 4567 | 7889 |
| 11 | toly.png | 5543 | 0 | 4567 | 7889 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
這樣獲取數據會比較方便些
$cursor = $conn->prepare($sql_query);//準備
$res = $cursor->execute([$pic_path, $pic_length]);//執行
$cursor->bindColumn(1, $id_col);
$cursor->bindColumn(2, $pic_path_col);
$cursor->bindColumn(3, $pic_length_col);
$cursor->bindColumn(4, $pic_mime_col);
$cursor->bindColumn(5, $pic_width_col);
$cursor->bindColumn(6, $pic_height_col);
if ($res) {
$table = "<table border='1' cellspacing='0' cellpadding='0' width='70%' >";
$table .= "<tr/>";
$table .= "<td >id</td>";
$table .= "<td >pic_path</td>";
$table .= "<td >pic_length</td>";
$table .= "<td >pic_mime</td>";
$table .= "<td >pic_width</td>";
$table .= "<td >pic_height</td>";
$table .= "</tr>";
while ($cursor->fetch()) {
$table .= "<tr/>";
$table .= "<td >" . $id_col . "</td>";
$table .= "<td >" . $pic_path_col . "</td>";
$table .= "<td >" . $pic_length_col . "</td>";
$table .= "<td >" . $pic_mime_col . "</td>";
$table .= "<td >" . $pic_width_col . "</td>";
$table .= "<td >" . $pic_height_col . "</td>";
$table .= "</tr>";
}
echo $table;
}
複製代碼
---->[pdo/config.php]---------------------配置文件--------------
<?php
define("DB_HOST", "localhost");
define("DB_PORT", "3306");
define("DB_USER", "root");
define("DB_PWD", "xxxxxx");
define("DB_NAME", "datatype");
define("DB_TYPE", "mysql");
define("DB_CHARSET", "utf8");
複製代碼
Pdor
屬性和構造函數
class Pdor{
private static $config = [];//配置
private static $conn;//鏈接
private static $pconn = false;//是否支持長鏈接
private static $dbInfo;//數據信息
private static $connected = false;//是否鏈接成功
private static $PDOStatement;//PDOStatement
//---------- 單例模式------------------------
private static $INSTANCE;
static function getInstance()
{
if (self::$INSTANCE) {
return self::$INSTANCE;
} else {
self::$INSTANCE = new self();
return self::$INSTANCE;
}
}
private function __construct($config = '')
//---------- 單例模式------------------------
if (!class_exists("PDO")) {
self::throwException("不支持PDO");
return;
}
if (!is_array($config)) {//構造方法未傳入配置 ,則使用配置文件構建$config變量
$config = [
'hostname' => DB_HOST,
'hostport' => DB_PORT,
'username' => DB_USER,
'password' => DB_PWD,
'database' => DB_NAME,
'dbms' => DB_TYPE,
'dsn' => DB_TYPE . ":host=" . DB_HOST . ";dbname=" . DB_NAME,
];
}
if (empty($config['hostname'])) {//構造方法未傳入配置,無配置文件
self::throwException("數據庫未配置");
return;
}
self::$config = $config;
if (empty(self::$config['params'])) {//params屬性爲空
self::$config['params'] = [];
}
if (!isset(self::$conn)) {//未鏈接
$configs = self::$config;
if (self::$pconn) {//設置是否正常長鏈接
$configs['params'][constant("PDO::ATTR_PERSISTENT")] = true;
}
try {//鏈接數據庫
self::$conn = new \PDO($configs['dsn'], $configs['username'], $configs['password']);
} catch (\Exception $e) {
self::throwException($e->getMessage());
}
if (!self::$conn) {//沒連上
self::throwException("鏈接異常");
return;
}
self::$conn->exec('SET NAMES ' . DB_CHARSET);//設置字符集
self::$dbInfo['version'] = self::$conn->getAttribute(constant('PDO::ATTR_SERVER_VERSION'));
self::$connected = true;
unset($configs);
}
}
/**異常處理
* @param $err
*/
private function throwException($err){
echo "<div style='text-align: center; width:70%;color:#fff;margin: 10px ;padding: 10px ; background-color: red ; border: blue 5px solid ; font-size: larger' > $err</div>";
}
}
複製代碼
public function queryAll($sql = null){
$this->query($sql);
$res = self::$PDOStatement->fetchAll(constant("PDO::FETCH_ASSOC"));
return $res;
}
/** 查詢
* @param null $sql
* @return bool
*/
public function query($sql = null){
self::freeStateIfNotNull();
$conn = self::$conn;
if ($sql != null && $conn) {
self::$querySQL = $sql;
self::$PDOStatement = $conn->prepare($sql);
$res = self::$PDOStatement->execute();
self::ifErrorHandleSQL($sql);// 若是sql語句有誤 打印
return $res;
}
}
/**
* 釋放結果集
*/
private function freeStateIfNotNull(){
if (!empty(self::$PDOStatement)) {
self::$PDOStatement = null;
}
}
/**
* 若是sql語句有誤 打印
*/
private function ifErrorHandleSQL($sql){
$err = empty(self::$PDOStatement) ? self::$conn : self::$PDOStatement;
$errArr = $err->errorInfo();
if ($errArr[0] != '00000') {
$err = '錯誤碼:' . $errArr[0] . '<br/>' . 'SQL錯誤信息 ' . $errArr[2] . '<br/>' . "ERROR ON : $sql";
self::throwException($err);
return false;
}
}
複製代碼
<?php
use lib\db\Pdor;
include '../lib/db/Pdor.php';
include './config.php';
$pdor = Pdor::getInstance();
$sql = 'SELECT * FROM php_pic;';
$all = $pdor->queryAll($sql);
print_r($all);
複製代碼
看一下錯誤的時候:能夠本身定義錯誤的樣式
$sql = 'SELECT * FROM php8_pic;';
複製代碼
---->[Pdor::queryRow]-----------------
/**查詢一條數據
* @param null $sql
* @return mixed
*/
public function queryRow($sql = null){
$this->query($sql);
$res = self::$PDOStatement->fetch(constant("PDO::FETCH_ASSOC"));
return $res;
}
|--- 使用
$sql_query_one = 'SELECT * FROM php_pic WHERE id=8;';
$one = $pdor->queryRow($sql_query_one);
print_r($one);
複製代碼
execute
此方法返回true/false
/**增刪改
* @param null $sql
* @return mixed
*/
public function execute($sql = null)
{
$conn = self::$conn;
self::freeStateIfNotNull();
if ($sql != null && $conn) {
self::$PDOStatement = $conn->prepare($sql);
$res = self::$PDOStatement->execute();
self::ifErrorHandleSQL($sql);// 若是sql語句有誤 打印
return $res;
}
return false;
}
複製代碼
exec
此方法返回改變的條數rowCount,和插入時的lastInsertId,更新和刪除lastInsertId=0;
/**增刪改
* @param null $sql
* @return mixed
*/
public function exec($sql = null)
{
$conn = self::$conn;
if ($sql != null && $conn) {
$len = $conn->exec($sql);//0
self::ifErrorHandleSQL($sql);// 若是sql語句有誤 打印
return [
'rowCount' => $len,
'lastInsertId' => $conn->lastInsertId(),
];
}
return false;
}
|--- 使用-----------------------------
$sql_insert = <<<EOT
INSERT INTO php_pic(pic_path,pic_length,pic_mime,pic_width,pic_height) VALUES
('30000X20000.jpg',116342886,1,30000,20000),
('3000X2000.jpg',3404969,1,3000,2000),
('300X200.jpg',99097,1,300,200),
('30X20.jpg',10158,1,30,20),
('6dc9e8455c47d964e1a8a4ef04cf9477.jpg',236254,1,974,319);
EOT;
$all = $pdor->exec($sql_insert);
print_r($all);
複製代碼
你覺得這就結束了?is just start !
好比根據指定的鍵,我想查三列,
$all = $pdor->queryByKey('php_pic', 19, ['pic_path', 'pic_length', 'pic_width']);
print_r($all);
|---- 封裝 -------------------------------
/**
* @param $table 表名
* @param $id 對應值
* @param string $attrs 屬性集
* @param string $key 索引
* @return mixed
*/
public function queryByKey($table, $id, $attrs = "*", $key = 'id'){
$sql = "SELECT %s FROM %s WHERE $key = %d";
$sql = sprintf($sql, $this->parseAttrs($attrs), $table, $id);
return $this->queryRow(sprintf($sql));
}
/**
* 解析屬性
*/
private function parseAttrs($attrs){
if (is_array($attrs)) {
array_walk($attrs, array('lib\db\Pdor', 'handleAttr'));
$res = implode(',', $attrs);
} else {
$res = "*";
}
return $res;
}
/**經過反引號將屬性括起來
* @param $value
* @return string
*/
public static function handleAttr(&$value){
if ($value === '*' || strpos($value, "." !== false || strpos($value, "`") != false)) {
} elseif (strpos($value, "`") == false) {
$value = '`' . trim($value) . '`';
}
return $value;
}
複製代碼
來個鏈式調用裝個13
$pdor->query('php_pic')->where("pic_height>500")->where("id>5")->where('pic_width>500')
->order('pic_width DESC')
->ok(['pic_path', 'pic_length', 'pic_width']);
複製代碼
封裝起來也挺簡單,不過感受不怎麼完美,有時間再推敲推敲
private $sql;
private $table = [];
private $where = [];
private $order = [];
private $having = [];
private $group;
public function submit($attrs = "*")
{
$where = '';
$order = '';
$group = '';
$having = '';
$head = 'SELECT ' . $this->parseAttrs($attrs) . ' FROM ' . $this->table;
if (!empty($this->where)) {
$where = $where . " WHERE ";
}
foreach ($this->where as $str) {
$where .= $str . ' AND ';
}
if (!empty($this->having)) {
$having = $having . " HAVING ";
}
foreach ($this->having as $str) {
$having .= $str . ' AND ';
}
foreach ($this->order as $str) {
$order .= " ORDER BY " . $str . ',';
}
$where = substr($where, 0, -4);
$having = substr($having, 0, -4);
$order = substr($order, 0, -1);
if (!empty($this->group)) {
$group = "GROUP BY " . $this->group;
}
$this->sql = $head . $where . $group . $having . $order . ";";
return $this->queryAll($this->sql);
}
public function query($table)
{
$this->table = $table;
return $this;
}
public function group($attr)
{
$this->group = $attr;
return $this;
}
public function where($where)
{
array_push($this->where, $where);
return $this;
}
public function having($having)
{
array_push($this->having, $having);
return $this;
}
public function order($order)
{
array_push($this->order, $order);
return $this;
}
複製代碼
$data = [
'pic_path' => 'hekko.png',
'pic_length' => 1994,
'pic_mime' => 0,
'pic_width' => 3,
'pic_height' => 28,
];
$pdor->add("php_pic", $data);
複製代碼
/**
* 用數組添加
*/
public function add($table, $data)
{
$keys = array_keys($data);//獲取鍵名
array_walk($keys, array('lib\db\Pdor', 'handleAttr'));
$resK = join(",", $keys);
$resV = array_values($data);
foreach ($resV as &$v) {
if (is_string($v)) {
$v = "'" . $v . "'";
}
}
$resV = join(",", $resV);
$sql = "INSERT INTO {$table} ({$resK}) VALUES ({$resV});";
echo $sql;
}
mysql> SELECT * FROM php_pic;
+----+--------------------------------------+------------+----------+-----------+------------+
| id | pic_path | pic_length | pic_mime | pic_width | pic_height |
+----+--------------------------------------+------------+----------+-----------+------------+
| 3 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 4 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 5 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 2333 | 10086 |
| 6 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 7 | 30000X20000.jpg | 116342886 | 1 | 30000 | 99 |
| 12 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 13 | 3000X2000.jpg | 3404969 | 1 | 3000 | 2000 |
| 14 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 15 | 30X20.jpg | 10158 | 1 | 2333 | 10086 |
| 16 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 974 | 319 |
| 17 | 30000X20000.jpg | 116342886 | 1 | 30000 | 20000 |
| 18 | 3000X2000.jpg | 3404969 | 1 | 3000 | 2000 |
| 19 | 300X200.jpg | 99097 | 1 | 300 | 200 |
| 20 | 30X20.jpg | 10158 | 1 | 30 | 20 |
| 21 | 6dc9e8455c47d964e1a8a4ef04cf9477.jpg | 236254 | 1 | 974 | 319 |
| 22 | hekko.png | 1994 | 0 | 3 | 28 |
+----+--------------------------------------+------------+----------+-----------+------------+
複製代碼
本篇就這樣,其餘的,根據字符串拼接的套路本身去玩吧