I have learned that the database class and pdo class encapsulated by myself, dbpdo

Source: Internet
Author: User
Tags dsn

I have learned that the database class and pdo class encapsulated by myself, dbpdo

DB Encapsulation

<? Phpclass DBDA {public $ host = "localhost"; public $ uid = "root"; public $ pwd = "root"; public $ dbname = "mydb "; public function Query ($ SQL, $ type = 1) // connect to the database. The Query result is displayed when the parameter is set to 1 by default, and the addition, deletion, modification, and other operations are performed. {$ Db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname ); $ result = $ db-> query ($ SQL); if ($ type = "1") {return $ result-> fetch_all ();} else {return $ result;} public function StrQuery ($ SQL, $ type = 1) // This method is used to splice the Retrieved Data (two-dimensional array) into strings, separated by ^ and |. {$ Db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname ); $ result = $ db-> query ($ SQL); if ($ type = "1") {$ arr = $ result-> fetch_all (); $ str = ""; foreach ($ arr as $ v) {$ str = $ str. implode ("^", $ v ). "|" ;}$ str = substr ($ str, 0, strlen ($ str)-1); return $ str ;}else {return $ result ;}} public function JsonQuery ($ SQL, $ type = 1) // use {$ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname); $ result = $ db-> query ($ SQL ); if ($ type = "1") {$ arr = $ result-> fetch_all (MYSQLI_ASSOC ); // change the parameter to the associated array return json_encode ($ arr);} else {return $ result ;}}}

 

PDO encapsulation class

<? Phpclass DBDAP {public $ host = "localhost"; public $ uid = "root"; public $ pwd = "root"; public $ dbname = "mydb "; public function Query ($ SQL, $ type = 1) {$ dsn = "mysql: dbname = $ this-> dbname; host = $ this-> host "; $ pdo = new PDO ($ dsn, "$ this-> uid", "$ this-> pwd"); $ result = $ pdo-> query ($ SQL ); if ($ type = "1") // when the parameter is 1, return the query result {return $ result-> fetchall (PDO: FETCH_ASSOC ); // The parameters in the brackets must be filled. Otherwise, the obtained data will be duplicated. If no data is written, the returned data is associated with the data and indexed.} Else {$ zeng = $ pdo-> prepare ($ SQL); if ($ type = "2") // when the parameter is 2, you can run the preprocessing statement {return $ zeng;} else {return $ result ;}} public function StrQuery ($ SQL, $ type = 1) // this method is used to splice the Retrieved Data (two-dimensional array) into a string {$ dsn = "mysql: dbname = $ this-> dbname; host = $ this-> host "; $ pdo = new PDO ($ dsn," $ this-> uid "," $ this-> pwd "); $ result = $ pdo-> query ($ SQL); if ($ type = "1") {$ arr = $ result-> fetchall (PDO: FETCH_ASSOC ); $ str = ""; foreach ($ arr as $ v) {$ str = $ str. implode ("^", $ v ). "|" ;}$ str = substr ($ str, 0, strlen ($ str)-1); return $ str ;} else {$ zeng = $ pdo-> prepare ($ SQL); if ($ type = "2") {return $ zeng;} else {return $ result ;}}} public function JsonQuery ($ SQL, $ type = 1) // This method is used in ajax to use {$ dsn = "mysql: dbname = $ this-> dbname; host = $ this-> host "; $ pdo = new PDO ($ dsn," $ this-> uid ", "$ this-> pwd"); $ result = $ pdo-> query ($ SQL); if ($ type = "1 ") {$ arr = $ result-> fetchall (PDO: FETCH_ASSOC); return json_encode ($ arr);} else {$ zeng = $ pdo-> prepare ($ SQL ); if ($ type = "2") {return $ zeng;} else {return $ result ;}}}}

 

Related Article

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.