mysql使用pdo簡單封裝select語句

2021-09-09 02:44:23 字數 3316 閱讀 1702

最終**:

1 function pdo_array_query($pdo, $table_name, $data, $fields=array('*'

))16 $codition_string = implode("

and

", $codition_array);

17 $fields_string = implode(", "

, $fields);

1819 $sql = "

select $fields_string from `$table_name` where

".$codition_string;

2021

//prepare our pdo statement.

22 $pdo_statement = $pdo->prepare($sql);

2324

foreach($to_bind as $param =>$val)

27//

return $pdo_statement->execute();

28 $pdo_statement->execute();

29 $result = $pdo_statement->fetchall();

30return

$result;

31//

print_r($result);

32 }

view code

用法:

$pdo = get_mysql_connect(); //

自定義 獲取pdo例項的方法

$table_name = 'order'; //

表名$fields = array("idproduct", "email", "customer", "address");//

需要查詢的字段

$data = array

( "idpayment"=>"1270117360-13027-963488",

"name"=>"john",

"age"=>"28",

"***"=>"boy",);

//查詢的條件

pdo_array_query($pdo, $table_name, $data, $fields);

拼合後的 sql 如下:

select idproduct, email, customer, address from `order` where idpayment = :idpayment and name = :name and age = :age and *** = :***
公升級版:

由於上面的方法,只能處理條件為 and 的情況,所以新方法增加了能新增 邏輯陣列的 引數,具體**如下:

1

function pdo_array_query($pdo, $table_name, $data, $fields=array('*'), $operators=array('and'))

15//

$codition_string = implode(" and ", $codition_array);

16if (count($operators) == 1)

19//

the operators should less one than condition_array

20else

if (count($condition_array) - count($operators) == 1)

2425

$fields_string = implode(", ", $fields

);26

27$sql = "select $fields_string from `$table_name` where ".$condition_string

;

2829

echo

$sql;30

//prepare our pdo statement.

31$pdo_statement = $pdo->prepare($sql

);32

foreach($to_bind

as$param => $val)35

$pdo_statement->execute();

36$result = $pdo_statement->fetchall();

37print_r($result

);38

//return $pdo_statement->rowcount();

39 }

view code

為了合理起見,關係陣列 應該是要比 字段陣列 少 1,如: 

idpayment = :idpayment and name = :name or age = :age and *** = :***

關係陣列是: array('and', 'or', 'and') 長度為3

字段陣列是: array('idpayment', 'name', 'age', '***') 長度為 4

陣列交叉合併的方法: 也可參考

1

function cross_merge_array($arr1, $arr2)2

1213

if ($i

< count($arr2

)) 16}17

return

$arr

;18 }

view code

用法:

$pdo = get_mysql_connect(); //

自定義 獲取pdo例項的方法

$table_name = 'order'; //

表名$fields = array("idproduct", "email", "customer", "address");//

需要查詢的字段

$data = array

( "idpayment"=>"1270117360-13027-963488",

"name"=>"john",

"age"=>"28",

"***"=>"boy",);

//查詢的條件

$operators = array

( 'and',

'or',

'and',);

pdo_array_query(

$pdo, $table_name, $data, $fields, $operators);

mysql簡單sql命令 mysql簡單sql命令

客戶端命令 status 檢視客戶端狀態 help 檢視所有客戶端可執行命令 伺服器端命令 1 檢視版本 select version 2 建立使用者並授權 create user root identified by mageedu.com grant all privileges on radi...

SqlSugar簡單使用封裝

一 sqlsugar簡單使用封裝 1.封裝讀取資料庫的方法 public static sqlsugarclient getinstance sqlsugarclient db new sqlsugarclient new connectionconfig db.aop.onlogexecuting...

簡單的mysql封裝類

class mysql 負責連線 private function connect host,user,pwd 負責切換資料庫,大的時候,可能用到不止乙個庫 public function switchdb dbname 負責設定字符集 public function setchar char 負責...