簡單的php寫入資料庫類

來源:互聯網
上載者:User

簡介:這是簡單的php寫入資料庫類的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340846' scrolling='no'>

 不知道原創要寫到隨筆裡。

All right ,第一篇博文。

有三個類:

    1 . 過濾輸入(輕量級的)  class input_filter

    2 . 轉換成SQL語句  

    3 . 資料庫查詢

 1   class input_filter
2 {
3
4 private $input_all; // 要過濾的數組
5 private $rustle; // 過濾後的結果
6
7 //建構函式 參數可以是$_GET or $_POST 這些
8 public function __construct($input_C)
9 {
10 if(is_array($input_C))
11 $this->input_all = $input_C ;
12 else
13 echo 'Parameter is not valid';
14
15 //初始化,不然後面第一次合并數組PHP不知道這是什麼類型
16 $this->rustle = array();
17 }
18
19 private function filter_arr() // 主函數
20 {
21
22 foreach ($this->input_all as $key_input => $val_input)
23 {
24 //如果鍵名不是字串,那麼返回錯誤資訊
25 // for key
26 if(!is_string($key_input)) // error
27 {
28 echo 'This key is not string';
29 return false;
30 }
31 // The # is mysql Note .
32 $key_one = str_replace('#','',$key_input);
33 $key = htmlspecialchars($key_one,ENT_QUOTES,'UTF-8');
34
35
36 // 我沒找 # 的HTML轉義符,所以用空代替
37 $val_one = str_replace('#','',$val_input);
38 // 這個函數只轉化 < > ' " ,還有個類似函數會轉義所有符號
39 $val = htmlspecialchars($val_one,ENT_QUOTES,'UTF-8');
40
41 // merger
42 $rustle_one = array($key=>$val);
43 //合并數組
44 $this->rustle = array_merge($this->rustle,$rustle_one);
45 }
46
47 }
48
49 //這個函數有點多餘,留下以後擴充用
50 public function get_filter_rustle()
51 {
52 $this->filter_arr();
53 return $this->rustle ;
54 }
55
56 }

 調用方法:             

  $filter = new filter_input($_GET) ; // or $_POST
$input_data = $filter->get_filter();

  

  轉換成SQL語句:  

 1   class write_db
2 {
3 private $Cnow_ary; // type array
4 private $Cname_str;
5
6 private $insert_sql; //最終的sql語句 string type
7
8 private $cols_db; // 列名 數組中是key
9 private $vals_db; // 插入值 value
10
11
12 public function __construct($Cary,$Cname)
13 {
14 //檢查傳入參數類型是否為數組
15 if (! is_array($Cary))
16 return false;
17 else
18 $this->Cnow_ary = $Cary; // 寫入的值
19
20 $this->Cname_str = $Cname; // 資料庫表名稱
21
22 $cols_db = ''; //初始化為字串
23 $vals_db = '';
24
25 }
26
27 private function setSql() // 主函數 ,生產SQL語句
28 {
29
30 foreach ( $this->Cnow_ary as $key_ary => $val_ary )
31 {
32 $this->cols_db = $this->cols_db.','.$key_ary; //列名組合
33 $this->vals_db = $this->vals_db.', \''.$val_ary.'\'' ; //值 組合
34 }
35 // 因為前面foreach的演算法有點問題,第一個字元是逗號
36 // 所以用sunstr_replace()刪除 ,自第一位起(0),只替換一個字元(1)
37 $this->cols_db = substr_replace($this->cols_db,'',0,1);
38 $this->vals_db = substr_replace($this->vals_db,'',0,1);
39
40 $this->insert_sql =
41 'INSERT INTO '.$this->Cname_str.' ( '
42 .$this->cols_db.' ) VALUES ( '.$this->vals_db.' )'; // 語句成型
43 }
44 //擴充用
45 public function getSql()
46 {
47 $this->setSql();
48 return $this->insert_sql;
49 }
50
51 }

 

    3 . 資料庫查詢

 資料庫查詢類是參照書上的單列模式(用靜態方法擷取對象,這樣在一個指令碼裡只有一個資料庫查詢類的執行個體)

我想單例模式用於這個類還是有點用的

 1   class mysql
2 {
3 private $connect;
4 static $objectMysql; // 存放對象
5
6 function __construct()
7 {
8 // 建立對象的時候這個建構函式會被調用,用來初始化
9 $connect = mysql_connect('db address','password','dbname');
10 $this->db = mysql_select_db('db',$connect);
11 }
12
13 public static function Mysql_object()
14 {
15 //instanceof 操作符用於檢查對象是否屬於某個類或者介面的執行個體。我說的不是很規範...
16 //如果$objectMysql不是mysql(self)的執行個體,那麼就建立一個
17 if(! self::$objectMysql instanceof self)
18 self::$objectMysql = new mysql();
19
20 //這時候的$objectMysql就已經是一個對象
21 return self::$objectMysql;
22 }
23 public function query($sql)
24 {
25 return mysql_query($sql,$this->db);
26 }
27
28 }

  

All right ,歸納一下

        

 1   $filter = new filter_input($_GET) ; // or $_POST
2 $input_data = $filter->get_filter();
3
4 $madeSql = new madesql($input_data,'tableName');
5 $sql = $madeSql->getSql();
6
7 $mysql = mysql::Mysql_object() ;
8 if( $mysql->query($sql) )
9 echo 'Ok';
10 else
11 echo 'failure';

    測試這些的時候似乎建構函式只能聲明為public ,否則會出現致命錯誤 ,但書本上我記得寫的private我很疑惑

  filter_input類 的結果可以直接用作 madesql類 的參數的 前提是 :

        表單的name必須和資料庫的列名相同,否則你就白看這麼多

        O(∩_∩)O哈哈~

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/340846.html pageNo:6

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.