A brief talk on PHP value MySQL operation class _php instance

Source: Internet
Author: User
Tags explode rtrim

talking about PHP value MySQL operations class

<?php/** * Created by Phpstorm. * User:administrator * DATE:2016/6/27 * time:18:55/Class mysqls{private $table//table Private $opt

 

  ; Public function __construct ($host, $user, $pwd, $name, $table _names) {$this->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 field in the table Public function Tbfield () {$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 has 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; Public Function Order ($order) {$this->opt[' order ']=is_string ($order)? "

    Order BY {$order} ': ' is not a string ';

 

 

  return $this; The 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-&

    gt;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 ')) di

    E ("cannot be null");

      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 name//var_dump ($fileds) in the array;

    $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 found ($field, $id) {$sql = "Select {$this->opt[' field ']} from {$this->table} {$ This->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 ($k, $v) =each ($arrs)) {$str = "{$k}=". "'

    {$v} ', ';

    } $str =rtrim ($str, ",");

 

    $sql = "Update {$this->table} set {$str} {$this->opt[' where ']}";

 

 

  return $this->querys ($sql); ///Get total record number 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 writing has a problem referring to others.

The above is small series for everyone to talk about PHP value MySQL operation class All content, I hope that we support cloud Habitat Community ~

Related Article

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.