设为首页 收藏本站
查看: 865|回复: 0

[经验分享] php数据库操作类

[复制链接]

尚未签到

发表于 2015-8-24 15:00:26 | 显示全部楼层 |阅读模式
<?php

002Class DB {

003  

004    private $link_id;

005    private $handle;

006    private $is_log;

007    private $time;

008  

009    //构造函数

010    public function __construct() {

011        $this->time = $this->microtime_float();

012        require_once("config.db.php");

013        $this->connect($db_config["hostname"], $db_config["username"], $db_config["password"], $db_config["database"], $db_config["pconnect"]);

014        $this->is_log = $db_config["log"];

015        if($this->is_log){

016            $handle = fopen($db_config["logfilepath"]."dblog.txt", "a+");

017            $this->handle=$handle;

018        }

019    }

020      

021    //数据库连接

022    public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8') {

023        if( $pconnect==0 ) {

024            $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);

025            if(!$this->link_id){

026                $this->halt("数据库连接失败");

027            }

028        } else {

029            $this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw);

030            if(!$this->link_id){

031                $this->halt("数据库持久连接失败");

032            }

033        }

034        if(!@mysql_select_db($dbname,$this->link_id)) {

035            $this->halt('数据库选择失败');

036        }

037        @mysql_query("set names ".$charset);

038    }

039      

040    //查询  

041    public function query($sql) {

042        $this->write_log("查询 ".$sql);

043        $query = mysql_query($sql,$this->link_id);

044        if(!$query) $this->halt('Query Error: ' . $sql);

045        return $query;

046    }

047      

048    //获取一条记录(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH)               

049    public function get_one($sql,$result_type = MYSQL_ASSOC) {

050        $query = $this->query($sql);

051        $rt =& mysql_fetch_array($query,$result_type);

052        $this->write_log("获取一条记录 ".$sql);

053        return $rt;

054    }

055  

056    //获取全部记录

057    public function get_all($sql,$result_type = MYSQL_ASSOC) {

058        $query = $this->query($sql);

059        $i = 0;

060        $rt = array();

061        while($row =& mysql_fetch_array($query,$result_type)) {

062            $rt[$i]=$row;

063            $i++;

064        }

065        $this->write_log("获取全部记录 ".$sql);

066        return $rt;

067    }

068      

069    //插入

070    public function insert($table,$dataArray) {

071        $field = "";

072        $value = "";

073        if( !is_array($dataArray) || count($dataArray)<=0) {

074            $this->halt('没有要插入的数据');

075            return false;

076        }

077        while(list($key,$val)=each($dataArray)) {

078            $field .="$key,";

079            $value .="'$val',";

080        }

081        $field = substr( $field,0,-1);

082        $value = substr( $value,0,-1);

083        $sql = "insert into $table($field) values($value)";

084        $this->write_log("插入 ".$sql);

085        if(!$this->query($sql)) return false;

086        return true;

087    }

088  

089    //更新

090    public function update( $table,$dataArray,$condition="") {

091        if( !is_array($dataArray) || count($dataArray)<=0) {

092            $this->halt('没有要更新的数据');

093            return false;

094        }

095        $value = "";

096        while( list($key,$val) = each($dataArray))

097        $value .= "$key = '$val',";

098        $value .= substr( $value,0,-1);

099        $sql = "update $table set $value where 1=1 and $condition";

100        $this->write_log("更新 ".$sql);

101        if(!$this->query($sql)) return false;

102        return true;

103    }

104  

105    //删除

106    public function delete( $table,$condition="") {

107        if( empty($condition) ) {

108            $this->halt('没有设置删除的条件');

109            return false;

110        }

111        $sql = "delete from $table where 1=1 and $condition";

112        $this->write_log("删除 ".$sql);

113        if(!$this->query($sql)) return false;

114        return true;

115    }

116  

117    //返回结果集

118    public function fetch_array($query, $result_type = MYSQL_ASSOC){

119        $this->write_log("返回结果集");

120        return mysql_fetch_array($query, $result_type);

121    }

122  

123    //获取记录条数

124    public function num_rows($results) {

125        if(!is_bool($results)) {

126            $num = mysql_num_rows($results);

127            $this->write_log("获取的记录条数为".$num);

128            return $num;

129        } else {

130            return 0;

131        }

132    }

133  

134    //释放结果集

135    public function free_result() {

136        $void = func_get_args();

137        foreach($void as $query) {

138            if(is_resource($query) && get_resource_type($query) === 'mysql result') {

139                return mysql_free_result($query);

140            }

141        }

142        $this->write_log("释放结果集");

143    }

144  

145    //获取最后插入的id

146    public function insert_id() {

147        $id = mysql_insert_id($this->link_id);

148        $this->write_log("最后插入的id为".$id);

149        return $id;

150    }

151  

152    //关闭数据库连接

153    protected function close() {

154        $this->write_log("已关闭数据库连接");

155        return @mysql_close($this->link_id);

156    }

157  

158    //错误提示

159    private function halt($msg='') {

160        $msg .= "\r\n".mysql_error();

161        $this->write_log($msg);

162        die($msg);

163    }

164  

165    //析构函数

166    public function __destruct() {

167        $this->free_result();

168        $use_time = ($this-> microtime_float())-($this->time);

169        $this->write_log("完成整个查询任务,所用时间为".$use_time);

170        if($this->is_log){

171            fclose($this->handle);

172        }

173    }

174      

175    //写入日志文件

176    public function write_log($msg=''){

177        if($this->is_log){

178            $text = date("Y-m-d H:i:s")." ".$msg."\r\n";

179            fwrite($this->handle,$text);

180        }

181    }

182      

183    //获取毫秒数

184    public function microtime_float() {

185        list($usec, $sec) = explode(" ", microtime());

186        return ((float)$usec + (float)$sec);

187    }

188}

189  

190?>

[代码] config.db.php



view source
http:///js/syntax-highlighter-2.1.382/scripts/clipboard.swfprint?

01<?php  

02    $db_config["hostname"] = "localhost"; //服务器地址

03    $db_config["username"] = "root"; //数据库用户名

04    $db_config["password"] = "123"; //数据库密码

05    $db_config["database"] = "test"; //数据库名称

06    $db_config["charset"] = "utf8";//数据库编码

07    $db_config["pconnect"] = 1;//开启持久连接

08    $db_config["log"] = 1;//开启日志

09    $db_config["logfilepath"] = './';//开启日志

10?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103599-1-1.html 上篇帖子: PHP的echo和print小谈 下篇帖子: php实现动态随机验证码机制(CAPTCHA)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表