php操作种

Jun 13, 2016 am 11:01 AM
gt link return table this

php操作类

<?php /**+---------------------- * Mysql操作类+---------------------- * 文件名称  Db.class.php+---------------------- * 文件描述  mysql操作类+---------------------- */class Db{    //数据库连接标识    protected $link = null;    //当前操作的表    public $table = '';    //查询参数    protected $options = array();    //当前执行的SQL语句    protected $sql = '';    //用什么编码传递数据    protected $dbCharset = 'utf8';    //缓存路径    protected $cachePath = './cache/';    //缓存扩展名    protected $cacheFileExt = "php";    //缓存文件名    protected $cacheFileName;    //是否缓存    protected $cache = false;    //缓存更新时间秒数    protected $cacheLimitTime = 60;    //数据返回类型, 1代表数组, 2代表对象    protected $returnType = 1;  /*   * 根据当前动态文件生成缓存文件名   */	function setCacheFileName($fileName) {		$cacheFileName = $this->cachePath . strtoupper(md5($fileName)).".".$this->cacheFileExt;		$this->cacheFileName=$cacheFileName;    }  /*   * 根据当前动态文件生成缓存文件名   */	function getCacheFileName() {		return  $this->cacheFileName;    }    /**     * 连接数据库     *     * @access      public     * @param       array    $db  数据库配置     * @return      resource 数据库连接标识     */    public function connect($db){        //根据配置使用不同函数连接数据库        $db['host'] = isset($db['port']) ? $db['host'].':'.$db['port']: $db['host'];        $db['char'] = isset($db['char']) ? $db['char']: $this->dbCharset;        $func = $db['pconnect'] ? 'mysql_pconnect' : 'mysql_connect';        $this->link = $func($db['host'], $db['user'], $db['pwd']);        mysql_select_db($db['database'], $this->link);        mysql_query("SET NAMES '{$db['char']}'");        $this->cachePath = isset($db['cachepath']) ? $db['cachepath']: $this->cachePath;        return $this->link;    }    /**     * 查询符合条件的一条记录     *     * @access      public     * @param       string    $where  查询条件     * @param       string    $field  查询字段     * @param       string    $table  表     * @return      mixed             符合条件的记录     */    public function find($where = NULL, $field = '*', $table = ''){        return $this->findAll($where = NULL, $field = '*', $table = '', FALSE);    }    /**     * 查询符合条件的所有记录     *     * @access      public     * @param       string    $where  查询条件     * @param       string    $field  查询字段     * @param       string    $table  表     * @return      mixed             符合条件的记录     */    public function findAll($where = NULL, $field = '*', $table = '', $all = TRUE){        $this->options['where'] = is_null($where) ? @$this->options['where']: $where;        $this->options['field'] = isset($this->options['field']) ? $this->options['field']: $field;        $this->options['table'] = $table == '' ? $this->table: $table;        $sql = "SELECT {$this->options['field']} FROM `{$this->options['table']}` ";        $sql .= isset($this->options['join']) ? ' LEFT JOIN '.$this->options['join']: '';        $sql .= isset($this->options['where']) ? ' WHERE '.$this->options['where']: '';        $sql .= isset($this->options['group']) ? ' GROUP BY '.$this->options['group']: '';        $sql .= isset($this->options['having']) ? ' HAVING '.$this->options['having']: '';        $sql .= isset($this->options['order']) ? ' ORDER BY '.$this->options['order']: '';        $sql .= isset($this->options['limit']) ? ' LIMIT '.$this->options['limit']: '';        $this->sql = $sql;        $row = NULL;        //如果开启了缓存, 那么重缓存中获取数据        if ($this->cache === TRUE){			$this->setCacheFileName($this->sql);            $row = $this->readCache();        }        //如果读取失败, 或则没有开启缓存        if (is_null($row)){            $result = $this->query();            $row = $all === TRUE ? $this->fetchAll($result): $this->fetch($result);            //如果开启了缓存, 那么就写入            if ($this->cache === TRUE){                $this->writeCache($row);            }            $this->options = array();        }        return $row;    }    /**     * 读取结果集中的所有记录到数组中     *     * @access public     * @param  resource  $result  结果集     * @return array     */    public function fetchAll($result = NULL){        $rows = array();        while ($row = $this->fetch($result)){            $rows[] = $row;        }        return $rows;    }    /**     * 读取结果集中的一行记录到数组中     *     * @access public     * @param  resource  $result  结果集     * @param  int       $type    返回类型, 1为数组, 2为对象     * @return mixed              根据返回类型返回     */    public function fetch($result = NULL, $type = NULL){        $result = is_null($result) ? $this->result: $result;        $type = is_null($type) ? $this->returnType: $type;        $func = $type === 1 ? 'mysql_fetch_assoc' : 'mysql_fetch_object';        return $func($result);    }    /**     * 执行SQL命令     *     * @access      public     * @param       string    $sql    SQL命令     * @param       resource  $link   数据库连接标识     * @return      mixed             数据库结果集     */    public function query($sql = '', $link = NULL){        $sql = empty($sql) ? $this->sql: $sql;        $link = is_null($link) ? $this->link: $link;        $this->result = mysql_query($sql, $link);        if (is_resource($this->result)){            return $this->result;        }        //如果执行SQL出现错误, 那么抛出异常        exit('<strong>Mysql error:</strong>'.$this->getError());    }    /**     * 执行SQL命令     *     * @access      public     * @param       string    $sql    SQL命令     * @param       resource  $link   数据库连接标识     * @return      bool              是否执行成功     */    public function execute($sql = '', $link = NULL){        $sql = empty($sql) ? $this->sql: $sql;        $link = is_null($link) ? $this->link: $link;        if (mysql_query($sql, $link)){            return TRUE;        }        return FALSE;    }    /**     * 插入记录     *     * @access public     * @param  array  $data  插入的记录, 格式:array('字段名'=>'值', '字段名'=>'值');     * @param  string $table 表名     * @return bool          当前记录id     */    public function add($data, $table = NULL){        $table = is_null($table) ? $this->table: $table;        $sql = "INSERT INTO `{$table}`";        $fields = $values = array();        $field = $value = '';        //遍历记录, 格式化字段名称与值        foreach($data as $key => $val){            $fields[] = "`{$table}`.`{$key}`";            $values[] = is_numeric($val) ? $val : "'{$val}'";        }        $field = join(',', $fields);        $value = join(',', $values);        unset($fields, $values);        $sql .= "({$field}) VALUES({$value})";        $this->sql = $sql;        $this->execute();        return $this->insertId();    }    /**     * 删除记录     *     * @access public     * @param  string  $where  条件     * @param  string  $table  表名     * @return bool            影响行数     */    public function delete($where = NULL, $table = NULL){        $table = is_null($table) ? $this->table: $table;        $where = is_null($where) ? @$this->options['where']: $where;        $sql = "DELETE FROM `{$table}` WHERE {$where}";        $this->sql = $sql;        $this->execute();        return $this->affectedRows();    }    /**     * 更新记录     *     * @access public     * @param  array   $data   更新的数据 格式:array('字段名' => 值);     * @param  string  $where  更新条件     * @param  string  $table  表名     * @return bool            影响多少条信息     */    public function update($data, $where = NULL, $table = NULL){        $table = is_null($table) ? $this->table: $table;        $where = is_null($where) ? @$this->options['where']: $where;        $sql = "UPDATE `{$table}` SET ";        $values = array();        foreach($data as $key => $val){            $val = is_numeric($val) ? $val : "'{$val}'";            $values[] = "`{$table}`.`{$key}` = {$val}";        }        $value = join(',', $values);        $this->sql = $sql.$value." WHERE {$where}";        $this->execute();        return $this->affectedRows();    }    /**     * 读取缓存     *     * @access      public     * @return      mixed   如果读取成功返回缓存内容, 否则返回NULL     */    protected function readCache(){        $file = $this->getCacheFileName();        if (file_exists($file)){            //缓存过期            if ((filemtime($file) + $this->cacheLimitTime) returnType){                $row = include $file;            }            else{                $data = file_get_contents($file);                $row = unserialize($data);            }            return $row;        }        return NULL;    }    /**     * 写入缓存     *     * @access      public     * @param       mixed   $data   缓存内容     * @return      bool            是否写入成功     */    public function writeCache($data){        $file = $this->getCacheFileName();        if ($this->makeDir(dirname($file))){            if (1 === $this->returnType){				$data = '<?php return '.var_export($data, TRUE).';?>';			}else{				$data = serialize($data);			}		}        return file_put_contents($file, $data);    }	/*	 * 清除缓存文件	 * string $fileName 指定文件名(含函数)或者all(全部)	 * 返回:清除成功返回true,反之返回false	 */	function clearCache( $fileName = "all" ) {		if( $fileName != "all" ) {			if( file_exists( $fileName ) ) {				return @unlink( $fileName );			}else return false;		}		if ( is_dir( $this->cachePath ) ) {			if ( $dir = @opendir( $this->cachePath ) ) {				while ( $file = @readdir( $dir ) ) {					$check = is_dir( $file );					if ( !$check )					@unlink( $this->cachePath . $file );				}				@closedir( $dir );				return true;			}else{				return false;			}		}else{		  return false;		}	}	  /*	   * 连续建目录	   * string $dir 目录字符串	   * int $mode   权限数字	   * 返回:顺利创建或者全部已建返回true,其它方式返回false	   */	function makeDir( $dir, $mode = "0777" ) {		if( ! $dir ) return 0;		$dir = str_replace( "\\", "/", $dir );				$mdir = "";		foreach( explode( "/", $dir ) as $val ) {			$mdir .= $val."/";			if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;		  			if( ! file_exists( $mdir ) ) {				if([email protected]( $mdir, $mode )){					return false;				}			}		}		return true;	}	//自动加载函数, 实现特殊操作    public function __call($func, $args)    {         if(in_array($func, array('field', 'join', 'where', 'order', 'group', 'limit', 'having')))         {               $this->options[$func] = array_shift($args);               return $this;         } elseif($func === 'table'){               $this->options['table'] = array_shift($args);               $this->table            = $this->options['table'];               return $this;         }        //如果函数不存在, 则抛出异常         exit('Call to undefined method Db::' . $func . '()');     }//-------------------------------------------        //返回上一次操作所影响的行数    public function affectedRows($link = null){		$link = is_null($link) ? $this->link : $link;		return mysql_affected_rows($link);    }        //返回上一次操作记录的id    public function insertId($link = null){        $link = is_null($link) ? $this->link : $link;        return mysql_insert_id($link);    }        //清空结果集    public function free($result = null){         $result = is_null($result) ? $this->result : $result;         return mysql_free_result($result);    }        //返回错误信息	public function getError($link = NULL){        $link = is_null($link) ? $this->link : $link;        return mysql_error($link);    }        //返回错误编号	public function getErrno($link = NULL){        $link = is_null($link) ? $this->link : $link;        return mysql_errno($link);    }}?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

C語言return的用法詳解 C語言return的用法詳解 Oct 07, 2023 am 10:58 AM

C語言return的用法有:1、對於傳回值類型為void的函數,可以使用return語句來提前結束函數的執行;2、對於傳回值型別不為void的函數,return語句的作用是將函數的執行結果傳回給呼叫者;3、提前結束函數的執行,在函數內部,我們可以使用return語句來提前結束函數的執行,即使函數並沒有回傳值。

修復:截圖工具在 Windows 11 中不起作用 修復:截圖工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

Java中return和finally語句的執行順序是怎樣的? Java中return和finally語句的執行順序是怎樣的? Apr 25, 2023 pm 07:55 PM

原始碼:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}#輸出上述程式碼的輸出可以簡單地得出結論:return在finally之前執行,我們來看下字節碼層面上發生了什麼事情。下面截取case1方法的部分字節碼,並且對照源碼,將每個指令的含義註釋在

如何修復無法連線到iPhone上的App Store錯誤 如何修復無法連線到iPhone上的App Store錯誤 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Vue3怎麼使用setup語法糖拒絕寫return Vue3怎麼使用setup語法糖拒絕寫return May 12, 2023 pm 06:34 PM

Vue3.2setup語法糖是在單文件組件(SFC)中使用組合式API的編譯時語法糖解決Vue3.0中setup需要繁瑣將聲明的變量、函數以及import引入的內容通過return向外暴露,才能在使用的問題1.在使用中無需return宣告的變數、函數以及import引入的內容,即可在使用語法糖//import引入的內容import{getToday}from'./utils'//變數constmsg='Hello !'//函數func

聊聊Vue2為什麼能透過this存取各種選項中屬性 聊聊Vue2為什麼能透過this存取各種選項中屬性 Dec 08, 2022 pm 08:22 PM

這篇文章帶大家解讀vue原始碼,來介紹一下Vue2中為什麼可以使用 this 存取各種選項中的屬性,希望對大家有幫助!

See all articles