PHP e-commerce shopping cart ——— Package database operation class
After the database is established, we still encapsulate a database of Operation class Bar (refer to the Mypdo written by the teacher, after the article appended a PDO summary in 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 (' config file does not exist ... '); } $this->pdo = new PDO ($DSN, $ini [' username '], $ini [' userpwd ']); }/** * destructor to destroy the PDO object at the same time */Public Function __destruct () {if ($this->pdo) $this->p do = null; The/** * receives the SQL (DML) statement and executes the number of rows returned * @param $sql incoming SQL statement String */Public Function pdoexec ($sql) {retur n $this->pdo->exec ($sql); The/** * receives a SQL (DQL) statement that executes and returns the query result without parameters, returns a two-dimensional array, or returns a null * @param $sql the incoming SQL statement string, if there is no result */public funct Ion Pdoquery ($sql) {$data = $this->pdo->query ($sql); if ($data) {return $data->fetchall (pdo::fetch_num); }else{return null; }}/** * Receive SQL (DQL) statement, which executes and returns the query result with no arguments, returns a two-dimensional array, or returns a null * @param $sql incoming SQL statement String * @param $objname Incoming 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, which takes parameters, executes and returns the result of the query, returns a two-dimensional array, or, if there is no result, returns a null * @param $sql the incoming SQL statement string * @param $arr Passed in 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 the SQL (DQL) statement, executes and returns the query result without parameters, returns a two-dimensional array, or returns a null * @param $sql the incoming SQL statement String * @param if there is no result $obj The name of the object passed in, string format * @param $arr incomingThe parameter value, 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 (and current class file under the same directory file)# # #数据库类型db =mysql## #数据库名称dbname =phpwork## #服务器地址或名称host =localhost## #数据库字符集charset =utf8## #数据库端口号port =3306### User name for operation username=xuzhengyang## #操作的用户密码userpwd =
The above describes the PHP e-commerce Shopping cart (II: Packaging Database Operation Class), including the content of the contents, I hope that the PHP tutorial interested in a friend helpful.