PHP value MySQL operation class, PHP value mysql_php tutorial

Source: Internet
Author: User
Tags rtrim

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;//}//field in table Public function t        Bfield () {$desc = $this->db->query ("desc {$this->table}");        $FIELDARR =array (); while (($row = $desc->fetch_assoc ())!=false) {$fieldarr[]= $row [' Field '];        }//Var_dump ($FIELDARR);    return $FIELDARR;         }//Query field Public function field ($filed) {//Split 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 if the field exists 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"; }}}//Conditional statement public function where ($where) {$this->opt[' where ']=is_string ($where)? "        WHERE {$where} ":" Is not a string ";    return $this; }//limit Public Function Limit ($limit) {$this->opt[' limit ']=is_string ($limit)? " Limit {$lImit} ":" Is not a string ";    return $this; The Public Function Order ($order) {$this->opt[' order ']=is_string ($order)? "        Order by {$order} ":" Is not a string ";    return $this; } Public Function Group ($group) {$this->opt[' group ']=is_string ($group)? "        Group by {$group} ":" Is not a string ";    return $this; }//Query string public function Select () {$sql = "select * from {$this->table} {$this->opt[' where ']} {$this-&G        t;opt[' Group '} {$this->opt[' Limit '} {$this->opt[' Order '} ";    return $this->fetch ($sql);        }//Result set query 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 statement public function Delete ($where =array ()) {        if ($where = = "" && empety ($this->opt[' where ')) die ("Cannot be empty");            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 statement Public Function Add ($filed) {if (!is_array ($filed)) die ("illegal array");  $fileds = $this->key (Array_keys ($filed)); Returns the key names 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); }//Find a single record public function find ($field, $id) {$sql = "Select {$this->opt[' field ']} from {$this->table} {$t His->where ($field. ' ='."'".        $id. "')}";        Var_dump ($sql);    return $this->fetch ($sql);        }//UPDATE statement public Function Save ($ARRS) {if (!is_array ($arrs)) die ("illegal array");        if (Empty ($this->opt[' where ')) die ("condition cannot be null");        $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);        }//Gets 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 (' 127.0.0.1 ', ' root ', ' ', ' MySQL ', ' user ');

Part of the writing has a problem referencing a part of others

http://www.bkjia.com/PHPjc/1138093.html www.bkjia.com true http://www.bkjia.com/PHPjc/1138093.html techarticle PHP value MySQL operation class, PHP value mysql? php/** * Created by Phpstorm. * User:administrator * DATE:2016/6/27 * time:18:55 */class My sqls{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.