PHP對Mysql操作的自訂函數

來源:互聯網
上載者:User

標籤:php   mysql   

 <?php/***@name db_connect 串連資料庫伺服器**@param string $host 主機地址*@param string $user 使用者名稱*@param string $pwd 使用者密碼*@param string $name 資料庫名*@param string $charset 字元集**@return mixed 資料庫連接*/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 向資料庫插入資料**@param string $link 串連地址*@param string $table 表*@param string $data 插入的資料**@return mixed true或者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;die;$result = mysqli_query($link, $sql);if ($result && mysqli_affected_rows($link)) {//返回本次插入的id(該表有自增的id欄位)return mysqli_insert_id($link);}  return false;}/***@name db_delete 刪除資料庫的資料**@param string $link 串連地址*@param string $table 表*@param string $where 條件**@return mixed      true或者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 更新資料庫的資料**@param string $link 串連地址*@param string $table 表*@param string $set 設定資訊*@param string $where 條件**@return mixed      true或者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 刪除資料庫的資料**@param string $link 串連地址*@param string $table 表*@param string $where 條件*@param string $fields 查詢欄位*@param string $where 條件*@param string $orderby 排序**@return mixed      返回資料*/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;}//輔助函數1:對字元類型進行處理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;}//輔助函數2:對數組進行遍曆function parse_set($set){foreach ($set as $key => $value) {$data[] = $key . ‘=‘ . parse_value($value);}return $data;}


本文出自 “你好我是森林” 部落格,請務必保留此出處http://chensenlin.blog.51cto.com/10559465/1844018

PHP對Mysql操作的自訂函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.