: PHP e-commerce shopping cart (2: encapsulated database operation class): PHP e-commerce shopping cart-encapsulated database operation class after the database is created, let's encapsulate the Operation class of a database (refer to the Mypdo written by Shoufu, and then add a PDO summary to the learning summary module ). & Lt; pre & gt; & lt; prenam PHP e-commerce shopping cart --- encapsulate database operations
After the database is created, we will encapsulate a database operation class (refer to the Mypdo written by Shoufu, and then add a PDO summary to the learning summary module ).
If (file_exists (_ DIR _. 'db. ini ') {$ ini = parse_ini_file ('Db. ini '); $ dsn = $ ini ['DB']. ": host = ". $ ini ['host']. "; dbname = ". $ ini ['dbname']. "; port = ". $ ini ['host']. "; charset = ". $ ini ['charset'];} else {die ('configuration file does not exist .... ') ;}$ this-> pdo = new PDO ($ dsn, $ ini ['username'], $ ini ['userpwd']);} /*** destructor, used to destroy the PDO object at the same time */public function _ destruct () {if ($ this-> pdo) $ this-> pdo = null;}/*** receive SQL (DML) statement and execution returns the affected number of rows * @ param $ SQL passed in SQL statement string */public function pdoExec ($ SQL) {return $ this-> pdo-> exec ($ SQL);}/*** receives SQL (DQL) statements. this statement runs without parameters and returns query results, the returned value is a two-dimensional array. If no result is returned, the return value is null * @ param $ the SQL statement string passed in by SQL */public function pdoQuery ($ SQL) {$ data = $ this-> pdo-> query ($ SQL); if ($ data) {return $ data-> fetchAll (PDO: FETCH_NUM );} else {return null;}/*** receives SQL (DQL) statements without parameters. execute and return the query results. The Returned values are two-dimensional arrays. If no results exist, return null * @ param $ SQL passed in SQL statement string * @ param $ objname passed in object name, string format */public function pdoQueryByObj ($ SQL, $ objname) {$ data = $ this-> pdo-> query ($ SQL); if ($ data) {$ datalist = array (); while ($ info = $ data-> fetchObject ($ objname) {array_push ($ datalist, $ info) ;}return $ datalist ;}else {return null ;}} /*** receives the SQL (DQL) statement. The statement carries parameters and runs and returns the query result. the returned value is a two-dimensional array. If no result is returned, returns the null * @ param $ SQL input SQL statement string * @ param $ arr input parameter value, array format */public function pdoPrepare ($ SQL, $ arr) {$ data = $ this-> pdo-> prepare ($ SQL); $ bl = $ data-> execute ($ arr); if ($ bl) {return $ data-> fetchAll (PDO: FETCH_NUM) ;}else {return null ;}/ *** receives SQL (DQL) statements without parameters, execute and return the query result. the returned value is a two-dimensional array. If no result is returned, null * @ param $ is returned for the SQL statement string passed in by SQL * @ param $ objname, string format * @ param $ parameter value passed in by arr, array format */public function pdoPrepareByObj ($ SQL, $ objname, $ arr) {$ data = $ this-> pdo-> prepare ($ SQL); $ bl = $ data-> execute ($ arr); if ($ bl) {$ datalist = array (); while ($ info = $ data-> fetchObject ($ objname) {array_push ($ datalist, $ info);} return $ datalist ;} else {return null ;}}}
Configuration file (in the same directory as the current class file)### Database type db = mysql ### database name dbname = phpwork ### server address or name host = localhost ### database character set charset = utf8 ### database port number = 3306 ### username of the operation = xuzhengyang ### userpwd =
The above introduces the PHP e-commerce shopping cart (2: Encapsulation of database operations), including the content, hope to be helpful to friends interested in PHP tutorials.