亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 44
粉絲 3
評(píng)論 3
訪問(wèn)量 40224
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
數(shù)據(jù)庫(kù)常用操作封裝: 連接,新增,更新,單條查詢,多條查詢,刪除--4月26日作業(yè)
唔良人
原創(chuàng)
792人瀏覽過(guò)

實(shí)例

<?php
//連接數(shù)據(jù)庫(kù)
if(!function_exists('connect')){
    /**
     * [mysql數(shù)據(jù)庫(kù)連接]
     * @param  [type]  $dbname  [description]
     * @param  string  $type    [description]
     * @param  string  $host    [description]
     * @param  string  $charset [description]
     * @param  integer $port    [description]
     * @param  string  $user    [description]
     * @param  string  $pass    [description]
     * @return [type]           [description]
     */
	function connect($dbname,$type='mysql',$host='127.0.0.1',$charset='utf8',$port=3306,$user='root',$pass='root')
	{
		$dsn="{$type}:host=[$host];dbname={$dbname};charset={$charset};port={$port}";
		$username=$user;
		$password=$pass;
		$options=[
		    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,//設(shè)置錯(cuò)誤模式
            PDO::ATTR_CASE=>PDO::CASE_NATURAL,//數(shù)據(jù)表字段保持不變,自然名稱
            PDO::ATTR_EMULATE_PREPARES=>true,//啟用PDO模擬
            PDO::ATTR_PERSISTENT=>true,//啟用持久性連接
        ];
        try{
        	//實(shí)例化PDO類,創(chuàng)建PDO對(duì)象
            $pdo = new PDO($dsn, $username, $password, $options);echo '數(shù)據(jù)庫(kù)連接成功(請(qǐng)刪除)<br>';
        } catch (PDOException $e) {
            die('Connect ERROR!:'.$e->getMessage());
        }

        return $pdo;
	}}

//插入數(shù)據(jù)
if(!function_exists('insert')){
	/**
	 * 增加數(shù)據(jù)
	 * @param  [type] $pdo   [description]
	 * @param  [type] $table [description]
	 * @param  array  $data  [description]
	 * @return [type]        [description]
	 */
	function insert($pdo,$table,$data=[])
	{
        //創(chuàng)建sql 語(yǔ)句
		$sql="INSERT IGNORE {$table} SET ";//數(shù)據(jù)庫(kù) 鍵值要設(shè)為唯一ignore 才有效果
        //提取數(shù)據(jù)中的鍵,放入sql
		foreach (array_keys($data) as $field) {
			$sql .=$field.' =:'.$field.',';
		}
		//去除“,”號(hào),加上“;”號(hào)
		$sql=rtrim(trim($sql),',').';';//die($sql);//檢測(cè)sql語(yǔ)句

		$yu=$pdo->prepare($sql);
        
        //綁定
		foreach ($data as $key => $value) {
			$yu->bindValue(":{$key}",$value);
		}
        //執(zhí)行
		if($yu->execute()){
			if($yu->rowCount()>0){
				return true;
			}
		}else{
			    return false;
		}
	}}

//刪除數(shù)據(jù)
if(!function_exists('delete')){
	
    /**
     * [delete description]
     * @param  [type] $pdo   [description]
     * @param  [type] $table [description]
     * @param  string $where [description]
     * @return [type]        [description]
     */
    function delete($pdo,$table,$where=''){
   
	$sql="DELETE FROM {$table} ";
    if (!empty($where)) {
    	$sql .='WHERE '.$where.';';
    }else{
    	exit('刪除數(shù)據(jù),條件不能為空');
    }

    //die($sql);


    $yu=$pdo->prepare($sql);
    
    if($yu->execute()){
    	if($yu->rowCount()>0){
    		echo '刪除成功';
    		return true;
    		}
    }else{
    	echo '刪除失敗';
    	return false;
    	exit();
    }
   }}

//修改數(shù)據(jù)
if (!function_exists('update')) {
	
	function update($pdo,$table,$data=[],$where=''){
		$sql="UPDATE {$table} SET  ";
		
		foreach (array_keys($data) as $key) {
			$sql.=$key.'=:'.$key.',';
		}
        
        $sql=rtrim(trim($sql),',');

		if (!empty($where)) {
    	    $sql .=' WHERE '.$where.';';
        }else{
    	       exit('修改數(shù)據(jù),條件不能為空');
             }
		// die($sql);
		
		$yu=$pdo->prepare($sql);

		foreach ($data as $key => $value) {
			$yu->bindValue(":{$key}",$value);
		}
		//die($sql);
				
		if($yu->execute()){
    	    if($yu->rowCount()>0){
    		    echo '修改成功';
    		    return true;
    		}
        }else{
    	    echo '修改失敗';
    	    return false;
 }}}

//單條語(yǔ)句查詢
if(!function_exists('one_select')){
/**
 * [one_select description]
 * @param  [type] $pdo    [description]
 * @param  [type] $table  [description]
 * @param  [type] $fields [description]
 * @param  string $where  [description]
 * @return [type]         [description]
 */
	function one_select($pdo,$table,$fields,$where=''){
		$sql="SELECT ";
		if(!empty($fields)){
		if(is_array($fields)){
			foreach ($fields as $key ) {
				$sql.=$key.',';
			}
		}else{
				$sql.=$fields;
		     }
		 }else{
		 	echo '沒(méi)有查詢字段';
		 	exit();
		 }
		$sql=rtrim(trim($sql),',');
		$sql.=' FROM '.$table;
		if(!empty($where)) {
            $sql .= '  WHERE '. $where.';';
        }else{
        	$sql .=' LIMIT 1;';//沒(méi)有條件,就顯示第一條記錄
        }

        //die($sql);
       $yu=$pdo->prepare($sql);
       if($yu->execute()){
       	   if($yu->rowCount()>0){
       	   	   $yu->setFetchMode(PDO::FETCH_ASSOC);
       	   	   return $yu->fetch();
       	   }
       }else{
       	   return false;
       }
   }
}

//多條語(yǔ)句查詢
if(!function_exists('duo_select')){
    /**
     * [duo_select description]
     * @param  [type] $pdo    [description]
     * @param  [type] $table  [description]
     * @param  [type] $fields [description]
     * @param  string $where  [description]
     * @param  string $order  [description]
     * @return [type]         [description]
     */
	function duo_select($pdo,$table,$fields,$where='',$order=''){
	    $sql='SELECT ';
	    if(is_array($fields)){
		    foreach($fields as $key){
			$sql.=$key.',';
		    }
	    }else{
			$sql.=$fields;
	         }
	    $sql=rtrim(trim($sql),',');
	    $sql.=' FROM '.$table;
	    if(!empty($where)){
		    $sql.=' WHERE '.$where;
	    }

	    if(!empty($order)){
		    $sql.=' order by '.$order;
	    }

	    $sql = rtrim(trim($sql),',').';';
        //die($sql);
        //創(chuàng)建PDO預(yù)處理對(duì)象
        $stmt = $pdo->prepare($sql);
        //die($yu->queryString);//查看
        //執(zhí)行查詢操作
        if($stmt->execute()){
            if($stmt->rowCount()>0){
                $stmt->setFetchMode(PDO::FETCH_ASSOC);
                //返回一個(gè)二維數(shù)組
                return $stmt->fetchAll();
            }
        } else {
            return false;
        }
    }
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


批改狀態(tài):未批改

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)