PHP's custom functions for MySQL operations

Source: Internet
Author: User
Tags mixed

 <?php/*** @name  db_connect  Connect to database server * * @param  string  $host   host address * @param   string  $user   User name * @param  string  $pwd   User password * @param  string  $name   database name *@ param string  $charset   Character Set * * @return  mixed  database connection */function db_connect ($host, $user, $pwd, $name, $charset) {$link  = mysqli_connect ($host, $user, $pwd);if  (! $link)  {return  false;}  if  (!mysqli_select_db ($link, $name))  {return false;} Mysqli_set_charset ($link, $charset);return  $link;}  /*** @name  db_insert  inserting data into a database * * @param  string  $link   Connection address * @param  string  $table   Table * @param  string  $data   inserted data * * @return  mixed true or False*/ function  db_insert ($link, $table, $data) {$keys  = join (', ',  array_keys ($data)); $values  =  Implode (', ',  parse_value (Array_values ($data))); $sql  =  "insert into  $table ($keys)  VALUES ($values) ";//echo  $sql;d ie $result  = mysqli_query ($link,  $sql);if  ($result  && mysqli_affected_rows ($link))  {//returns the ID of this insertion (the table has an ID field that has been increased) return mysqli_insert_id ($ link);}   return false;} /*** @name  db_delete  Delete data from a database * * @param  string  $link   Connection address * @param  string  $table   Table * @param  string  $where   conditions * * @return  mixed       True or False*/function db_delete ($link, $table, $where) {$sql  =  delete from  $table   where  $where "; $result  = mysqli_query ($link, $sql);if  ($result  && mysqli_ Affected_rows ($link))  {return true;} Return false;} /*** @name  db_delete  Update data for database * * @param  string  $link   Connection address * @param  string  $table   * @param  string  $set   Settings information * @param  string  $where   conditions * * @return  mixed       True or False*/function db_update ($link, $table, $set, $where) {if  (Is_array ($set))  {$set  =  join (', ',  parse_set ($set));} $sql  =  "update  $table  set  $set  where  $where"; $result  = mysqli_ Query ($link,  $sql);if  ($result  && mysqli_affected_rows ($link))  {return  true;} Return false;} /*** @name  db_delete  Delete data from a database * * @param  string  $link   Connection address * @param  string  $table   Table * @param  string  $where   conditions * @param  string  $fields   query fields * @param  string   $where   conditions * @param  string  $orderby   Sort * * @return  mixed        return Data */function db_select ($link, $table, $fields,  $where =null,  $orderby =null) {if  (Is_array ($fields))  {$fields  = implode (', ', $fields);} $sql  =  "select  $fields  from  $table";if  ($where)  {$sql  .=  "  where  $where ";} if  ($orderby)  {$sql  .=  " order by  $orderby";} $result  = mysqli_query ($link, $sql);if  ($result  && mysqli_affected_rows ($link) )  {while  ($row  = mysqli_fetch_assoc ($result))  {$data [] =  $row;} return  $data;}  return false;} Auxiliary function 1: processing of character Types Function parse_value ($data) {if  (is_string ($data))  {$data  =  ' \ '  .  $data  .  ';}  else if  (Is_array ($data))  {$data  = array_map (' Parse_value ',  $data);}  else if  (Is_null ($data))  {$data  =  ' null ';} return  $data;} Auxiliary function 2: Traversing an array function parse_set ($set) {foreach  ($set  as  $key  =>  $value)  {$data [] =  $key  .  ' = '  . parse_value ($value);} return  $data;}


This article is from the "Hello I am Forest" blog, please make sure to keep this source http://chensenlin.blog.51cto.com/10559465/1844018

PHP's custom functions for MySQL operations

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.