PHP e-commerce shopping cart (2: encapsulated database operations)

Source: Internet
Author: User
: 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.

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.