MySQL about database operation function encapsulation detailed

Source: Internet
Author: User
today's PHP operations database, mostly with MYSQLI implementation, and in the implementation of MYSQLI operation database, in the e-commerce management, is also very easy to use, since the use of the frequency is so high, it has to be a function library encapsulation, there is no class method to encapsulate, or simply using a structure-oriented approach, a unified summary of:

(1) Connect to the database:


function Connect () {    @ $link = Mysqli_connect (DB_HOST,DB_USER,DB_PWD);//Connect    to Database Mysqli_set_charset ($link, db_ CHARSET);//Set Database font format    mysqli_select_db ($link, Db_dbname) or Die (' database open failed ');//Select Database    if (mysqli_connect_errno ())    {die        (' Database connection failed: '. Mysqli_connect_errno ());    }    return $link;}

(2) Inserting data


function Insert ($link, $table, $array) {    $keys = join (', ', Array_keys ($array));    $values = "'". Join (', ', Array_values ($array)). "'";    $sql = ' insert INTO {$table} ({$keys}) VALUES ({$values}) ';    Mysqli_query ($link, $sql);    Return mysqli_insert_id ($link);}


(3) Modify the data



function Update ($link, $table, $array, $where = null) {     $setstr = ';      foreach ($array as $key = = $value)      {        $setstr. = ('. $key. ') = '. $value. ' ');         if (! $setstr) {              $sep = ';        } else{              $sep = ', ';         }         $setstr. = $sep. $key. ' ='."'" $value "'";     }      $sql = "Update {$table} set {$setstr}". ($where? ' WHERE '. $where: ');      Mysqli_query ($link, $sql);      Return Mysqli_affected_rows ($link); }

(4) Delete data


function Delete ($link, $table, $where = null) {    $sql = "Delete from {$table}". $where? ' where '. $where: ');    Mysqli_query ($link, $sql);    Return Mysqli_affected_rows ($link); }

(5) Find a record


function Fetchone ($link, $sql, $result _type=mysqli_assoc) {    $result = Mysqli_query ($link, $sql);    $row = Mysqli_fetch_array ($result, $result _type);    return $row;}

(6) Find more than one record


function Fetchall ($link, $sql, $result _type=mysqli_assoc) {    $result = Mysqli_query ($link, $sql);    while (@ $row = Mysqli_fetch_array ($result, $result _type))    {        $row [] = $row;    }    return $row;}

(7) Returns the number of rows in the result set


function getresultnum ($link, $sql) {$result = Mysqli_query ($link, $sql); Return Mysqli_num_rows ($result);} 
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.