PHP Database Write operations class

Source: Internet
Author: User

PHP Tutorial Database Tutorial Write operations class

There are three classes:

1. Filter input (Lightweight)

Class Input_filter

Responsible for the filtering of parameters such as $_get,$_post

Returns the array of value types used as parameters for the Made_sql class

2. Convert to SQL statement

Class Made_sql

The type of the parameter is the array and table name (string), and the array's key name is the column name of the table, and the value is the inserted value

The return value type is a string and is used as a parameter to the MySQL tutorial->query method

3. Database query

Class MySQL

Using a single-column mode, the static method to obtain the object, see the role of the instanceof operator


1 class Input_filter
2 {
3
4 private $input _all; The array to filter
5 Private $rustle; Filtered Results
6
7//constructor parameters can be $_get or $_post these
8 Public Function __construct ($input _c)
9 {
Ten if (Is_array ($input _c))
One $this->input_all = $input _c;
Or else
Echo ' Parameter is not valid ';
14
15//initialization, otherwise the first time after merging the array PHP doesn't know what kind of this is.
$this->rustle = Array ();
17}
18
Private Function Filter_arr ()//main function
20 {
21st
foreach ($this->input_all as $key _input => $val _input)
23 {
24//If the key name is not a string, then return the error message
/For Key
if (!is_string ($key _input))//Error
27 {
The echo ' This key is not string ';
return false;
30}
The # is MySQL note.
$key _one = str_replace (' # ', ', ', $key _input);
$key = Htmlspecialchars ($key _one,ent_quotes, ' UTF-8 ');
34
35//I didn't find the HTML escape character for #, so I used null instead
Panax Notoginseng $val _one = str_replace (' # ', ', ', $val _input);
38//This function only converts < > ' ", and a similar function escapes all symbols
$val = Htmlspecialchars ($val _one,ent_quotes, ' UTF-8 ');
40
A//merger
$rustle _one = Array ($key => $val);
43//Merge array
$this->rustle = Array_merge ($this->rustle, $rustle _one);
45}
46
47}
48
49//This function is a bit redundant, leaving later to expand with
Public Function Get_filter_rustle ()
51 {
$this->filter_arr ();
$this->rustle;
54}
55
56}

Call Method:

$filter = new Filter_input ($_get); or $_post
$input _data = $filter->get_filter ();

Convert to SQL statement:

1 class Madesql
2 {
3 Private $Cnow _ary; Parameter passed in type array
4 private $Cname _str;
5
6 private $insert _sql; The final SQL statement string type
7
8
9
10
11
Public function __construct ($Cary, $Cname)
13 {
14//Check whether the incoming parameter type is an array
if (! Is_array ($Cary))
return false;
Or else
$this->cnow_ary = $Cary; The value written
19
$this->cname_str = $Cname; database table name
21st

25}
26
The Private Function SetSQL ()//main functions, production SQL statements
28 {
29
foreach ($this->cnow_ary as $key _ary => $val _ary)
31 {
$cols _sql = $cols _sql. ', '. $key _ary; Column name combination
$vals _sql = $vals _sql. ', $val _ary. ' Value Combination
34}
35//Because the previous foreach algorithm has a problem, the first character is a comma
36//So with Sunstr_replace () deleted, from the first bit (0), only one character (1)
Panax Notoginseng $cols _sql = Substr_replace ($vals _sql, ', 0, 1);
$vals _sql = Substr_replace ($vals _sql, ', 0, 1);
39
$this->insert_sql =
' INSERT into '. $this->cname_str. '
$cols _sql. ') VALUES ('. $vals _sql. ') '; Statement Molding
43}
44//Extended Use
GetSQL Public Function ()
46 {
$this->setsql ();
$this->insert_sql;
49}
50
51}

3. Database query

A database query class is a single-column pattern on a reference book (a static method gets the object, so that there is only one instance of the database query class in a script)

I think a single case pattern is useful for this class.

1 class MySQL
2 {
3 private $connect;
4 static $objectMysql; Storing objects
5
6 Private Function __construct ()

7 {
8//When the object is created, this constructor is invoked to initialize the
9 $connect = mysql_connect (' DB address ', ' password ', ' dbname ');
$this->db = mysql_select_db (' db ', $connect);
11}
12
The public static function Mysql_object ()
14 {
The//instanceof operator is used to check whether an object belongs to an instance of a class or interface. What I'm saying is not very normative ...
16//If $objectmysql is not an instance of MySQL (self), create a
if (! Self:: $objectMysql instanceof Self)
Self:: $objectMysql = new MySQL ();
19
20//This time the $objectmysql is already an object
Return self:: $objectMysql;
22}
Public Function query ($sql)
24 {
mysql_query ($sql, $this->db);
26}
27
28}

All right, sum up the use method

1 $filter = new Filter_input ($_get); or $_post http://www.111cn.net
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 ';
Ten Else
One echo ' failure ';

You need only these lines of calling code to complete the operation of writing to the database

In addition to the private public problems of the constructor, the MySQL single example schema in the book is a constructor declared for private, but not a single instance of a class that produces a compilation error, that is, PHP cannot create an object and check it out.

The reason is that the creation of objects is often done outside the class, causing problems that cannot be accessed by constructors. A single-column mode creates an object in its own class, so there is no restriction on accessing the private method.

The fact that the single case pattern was supposed to prevent the creation of the same object now seems to encapsulate the constructor in a single case pattern, which does improve security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.