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 ;}}}}