PHP value mysql operation class, php value mysql_PHP tutorial

Source: Internet
Author: User
PHP value mysql operation class, php value mysql. PHP value mysql operation class, php value mysql? Php *** CreatedbyPhpStorm. * User: Administrator * Date: 2016627 * Time: 18: 55 * ClassMysqls {private $ table; table private $ opt; publicf PHP value mysql operation class, php value mysql
 Db = new mysqli ($ host, $ user, $ pwd, $ name); // database connection if (mysqli_connect_errno () {echo "database connection error ". mysqli_connect_errno (); exit () ;}$ this-> db-> query ("set names utf8"); $ this-> table = $ table_names; $ this-> opt ['field'] = "*"; $ this-> opt ['where'] = $ this-> opt ['order'] = $ this-> opt ['limit'] = $ this-> opt [' group '] = ''; // var_dump ($ this-> opt ['where']); // $ this-> M ($ table_names );} // database connection // protected function M ($ table_name ){ // $ This-> db = new mysqli (DBHOST, DBUSER, DBPWD, DBNAME); // if (mysqli_connect_errno () {// echo "database connection error ". mysqli_connect_errno (); // exit (); //} // $ this-> db-> query ("set names utf8 "); // $ this-> table = $ table_name; // The public function tbField () field in the table () {$ desc = $ this-> db-> query ("DESC {$ this-> table}"); $ fieldArr = array (); while ($ row = $ desc-> fetch_assoc ())! = False) {$ fieldArr [] = $ row ['field'];} // var_dump ($ fieldArr); return $ fieldArr ;} // query the public function field ($ filed) {// split the field $ filedArr = is_string ($ filed )? Explode (",", $ filed): $ filed; if (is_array ($ filedArr) {$ filed = ''; foreach ($ filedArr as $ v) {$ filed. = "'". $ v. "'". "," ;}}// var_dump ($ filed); return rtrim ($ filed, ",") ;}// determine whether the field has a public function isfield ($ fileds) {$ filedArr = is_string ($ fileds )? Explode (",", $ fileds): $ fileds; $ tbFiled = $ this-> tbField (); // var_dump ($ tbFiled); foreach ($ filedArr as $ v) {if (! In_array ($ v, $ tbFiled) {echo "field input error" ;}}// condition statement public function where ($ where) {$ this-> opt ['where'] = is_string ($ where )? "WHERE {$ where}": "Not a string"; return $ this;} // Limit public function Limit ($ limit) {$ this-> opt ['limit'] = is_string ($ Limit )? "Limit {$ limit}": "Not a string"; return $ this;} public function Order ($ order) {$ this-> opt ['order'] = is_string ($ Order )? "Order By {$ order}": "Not a string"; return $ this;} public function Group ($ group) {$ this-> opt ['group'] = is_string ($ Group )? "Group BY {$ group}": "Not a string"; return $ this;} // query the public function select () string () {$ SQL = "select * from {$ this-> table} {$ this-> opt ['where']} {$ this-> opt ['group']} {$ this-> opt ['limit'] }{$ this-> opt ['order']} "; return $ this-> fetch ($ SQL);} // query the result set public function fetch ($ SQL) {$ result = $ this-> db-> query ($ SQL); $ sqlarr = array (); while ($ row = $ result-> fetch_assoc ())! = False) {$ sqlarr [] = $ row;} // var_dump ($ sqlarr); return $ sqlarr;} // No result set query public function querys ($ SQL) {$ sqls = $ this-> db-> query ($ SQL); return $ this-> db-> affected_rows ;} // delete the statement public function delete ($ where = array () {if ($ where = "" & empety ($ this-> opt ['where']) die ("cannot be blank"); if ($ where! = "") {If (is_array ($ where) {$ where = implode (",", $ where );} $ this-> opt ['where'] = "where id IN ({$ WHERE })";} $ SQL = "delete from {$ this-> table} {$ this-> opt ['where']} {$ this-> opt ['limit']}"; var_dump ($ SQL); // return $ this-> query ($ SQL);} // array key name public function key ($ key) {if (! Is_array ($ key) die ("illegal array"); $ keys = ""; foreach ($ key as $ v) {$ keys. = $ v. ",";} return rtrim ($ keys, ",") ;}// array value public function value ($ value) {if (! Is_array ($ value) die ("illegal array"); $ strvalue = ""; foreach ($ value as $ v) {$ strvalue. = "'". $ v. "'". "," ;}return rtrim ($ strvalue, ",") ;}// add the public function add ($ filed) {if (! Is_array ($ filed) die ("invalid array"); $ fileds = $ this-> key (array_keys ($ filed )); // return the key name in the array // var_dump ($ fileds); $ values = $ this-> value (array_values ($ filed )); $ SQL = "insert into {$ this-> table} ({$ fileds}) VALUES ($ values)"; // var_dump ($ SQL ); return $ this-> querys ($ SQL);} // search for a single record public function find ($ field, $ id) {$ SQL = "select {$ this-> opt ['field']} from {$ this-> table} {$ this-> where ($ field. '= '. "'". $ id. "'")} "; var_du Mp ($ SQL); return $ this-> fetch ($ SQL);} // update the public function save ($ arrs) {if (! Is_array ($ arrs) die ("invalid array"); // if (empty ($ this-> opt ['where']) die ("condition cannot be blank"); $ str = ""; while (list ($ k, $ v) = each ($ arrs )) {$ str = "{$ k} = ". "'{$ v}'," ;}$ str = rtrim ($ str ,","); $ SQL = "Update {$ this-> table} set {$ str} {$ this-> opt ['where']}"; return $ this-> querys ($ SQL);} // obtain the total number of records public function counts () {$ SQL = "select 'id' from {$ this-> table} {$ this-> opt ['where']}"; // var_dump ($ SQL ); return $ this-> querys ($ SQL) ;}$ db = new Mysqls ('2017. 0.0.1 ', 'root', '', 'mysql', 'User ');

For some of the written questions, refer to others.

Why? Php/*** Created by PhpStorm. * User: Administrator * Date: 2016/6/27 * Time: 18:55 */Class Mysqls {private $ table; // table private $ opt; public f...

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.