简单的php写入数据库类

简介:这是简单的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
原文地址:https://www.cnblogs.com/ooooo/p/2243931.html