搜索
首页后端开发php教程php mysql数据库操作类_php实例

复制代码 代码如下:

/*
 *    mysql数据库 DB类
 *    @package    db
 *    @author        yytcpt(无影)
 *    @version    2008-03-27
 *    @copyrigth    http://www.d5s.cn/ 
 */
class db {
    var $connection_id = "";
    var $pconnect = 0;
    var $shutdown_queries = array();
    var $queries = array();
    var $query_id = "";
    var $query_count = 0;
    var $record_row = array();
    var $failed = 0;
    var $halt = "";
    var $query_log = array();
    function connect($db_config){
        if ($this->pconnect){
            $this->connection_id = mysql_pconnect($db_config["hostname"], $db_config["username"], $db_config["password"]);
        }else{
            $this->connection_id = mysql_connect($db_config["hostname"], $db_config["username"], $db_config["password"]);
        }
        if ( ! $this->connection_id ){
            $this->halt("Can not connect MySQL Server");
        }
        if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){
            $this->halt("Can not connect MySQL Database");
        }
        if ($db_config["charset"]) {
            @mysql_unbuffered_query("SET NAMES '".$db_config["charset"]."'");
        }
        return true;
    }
    //发送SQL 查询,并返回结果集
    function query($query_id, $query_type='mysql_query'){
        $this->query_id = $query_type($query_id, $this->connection_id);
        $this->queries[] = $query_id;
        if (! $this->query_id ) {
            $this->halt("查询失败:\n$query_id");
        }
        $this->query_count++;
        $this->query_log[] = $str;
        return $this->query_id;
    }
    //发送SQL 查询,并不获取和缓存结果的行
    function query_unbuffered($sql=""){
        return $this->query($sql, 'mysql_unbuffered_query');
    }
    //从结果集中取得一行作为关联数组
    function fetch_array($sql = ""){
        if ($sql == "") $sql = $this->query_id;
        $this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC);
        return $this->record_row;
    }
    function shutdown_query($query_id = ""){
        $this->shutdown_queries[] = $query_id;
    }
    //取得结果集中行的数目,仅对 INSERT,UPDATE 或者 DELETE
    function affected_rows() {
        return @mysql_affected_rows($this->connection_id);
    }
    //取得结果集中行的数目,仅对 SELECT 语句有效
    function num_rows($query_id="") {
        if ($query_id == "") $query_id = $this->query_id;
        return @mysql_num_rows($query_id);
    }
    //返回上一个 MySQL 操作中的错误信息的数字编码
    function get_errno(){
        $this->errno = @mysql_errno($this->connection_id);
        return $this->errno;
    }
    //取得上一步 INSERT 操作产生的 ID
    function insert_id(){
        return @mysql_insert_id($this->connection_id);
    }
    //得到查询次数
    function query_count() {
        return $this->query_count;
    }
    //释放结果内存
    function free_result($query_id=""){
           if ($query_id == "") $query_id = $this->query_id;
        @mysql_free_result($query_id);
    }
    //关闭 MySQL 连接
    function close_db(){
        if ( $this->connection_id ) return @mysql_close( $this->connection_id );
    }
    //列出 MySQL 数据库中的表
    function get_table_names(){
        global $db_config;
        $result = mysql_list_tables($db_config["database"]);
        $num_tables = @mysql_numrows($result);
        for ($i = 0; $i             $tables[] = mysql_tablename($result, $i);
        }
        mysql_free_result($result);
        return $tables;
       }
    //从结果集中取得列信息并作为对象返回,取得所有字段
    function get_result_fields($query_id=""){
           if ($query_id == "") $query_id = $this->query_id;
        while ($field = mysql_fetch_field($query_id)) {
            $fields[] = $field;
        }
        return $fields;
       }
    //错误提示
    function halt($the_error=""){
        $message = $the_error."
\r\n";
        $message.= $this->get_errno() . "
\r\n";
        $sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")";
        @mysql_unbuffered_query($sql);
        if (DEBUG==true){
            echo "MySQL 数据库错误";
            echo "\r\n";
            echo "\r\n";
            echo "
\r\n";
            echo "\r\n";
            echo "
\r\n";
            exit;
        }
    }
    function __destruct(){
        $this->shutdown_queries = array();
        $this->close_db();
    }
    function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
        $sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:"");
        return $sql;
    }
    function sql_insert($tbname, $row){
        foreach ($row as $key=>$value) {
            $sqlfield .= $key.",";
            $sqlvalue .= "'".$value."',";
        }
        return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")";
    }
    function sql_update($tbname, $row, $where){
        foreach ($row as $key=>$value) {
            $sqlud .= $key."= '".$value."',";
        }
        return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where;
    }
    function sql_delete($tbname, $where){
        return "DELETE FROM `".$tbname."` WHERE ".$where;
    }
    //新增加一条记录
    function row_insert($tbname, $row){
        $sql = $this->sql_insert($tbname, $row);
        return $this->query_unbuffered($sql);
    }
    //更新指定记录
    function row_update($tbname, $row, $where){
        $sql = $this->sql_update($tbname, $row, $where);
        return $this->query_unbuffered($sql);
    }
    //删除满足条件的记录
    function row_delete($tbname, $where){
        $sql = $this->sql_delete($tbname, $where);
        return $this->query_unbuffered($sql);
    }
    /*    根据条件查询,返回所有记录
     *    $tbname 表名, $where 查询条件, $limit 返回记录, $fields 返回字段
     */
    function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
        $sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort);
        return $this->row_query($sql);
    }
    //详细显示一条记录
    function row_select_one($tbname, $where, $fields="*", $orderby="id"){
        $sql = $this->sql_select($tbname, $where, 1, $fields, $orderby);
        return $this->row_query_one($sql);
    }
    function row_query($sql){
        $rs     = $this->query($sql);
        $rs_num = $this->num_rows($rs);
        $rows = array();
        for($i=0; $i            $rows[] = $this->fetch_array($rs);
        }
        $this->free_result($rs);
        return $rows;
    }
    function row_query_one($sql){
        $rs     = $this->query($sql);
        $row = $this->fetch_array($rs);
        $this->free_result($rs);
        return $row;
    }
    //计数统计
    function row_count($tbname, $where=""){
        $sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:"");
        $row = $this->row_query_one($sql);
        return $row["row_sum"];
    }
}
?>

很久没有发帖了,把我常用的一些php类文件分享出来。
如果您加了新功能,或者是有改进,请与大家一起分享。
复制代码 代码如下:

    $db_config["hostname"]    = "127.0.0.1";    //服务器地址
    $db_config["username"]    = "root";        //数据库用户名
    $db_config["password"]    = "root";        //数据库密码
    $db_config["database"]    = "wap_blueidea_com";        //数据库名称
    $db_config["charset"]        = "utf8";
    include('db.php');
    $db    = new db();
    $db->connect($db_config);
    //例:查询表 table_name 中 cid=1的所有记录。
    $row = $db->row_select('table_name', 'cid=1');
?>

更详细的使用方法,请参考 db类文件中的注释。 
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用