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

[经验分享] php连接Access类

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-28 08:48:59 | 显示全部楼层 |阅读模式
  <?php
/*
* Created on 2008-10-25
*
* developer by Alex.do QQ:20779512
* PHP 5.0
*/

   class mdbClass {
    var $dbPath = 'database/#123123#.mdb';    //数据库路径
    var $tabName;            //表名
    var $aryChar;            //写入、查询操作时为列的集合,更新操作时为更新具体内容
    var $aryText;            //写入操作时为值的集合,更新、删除、查询操作时为更新的条件,批量请赋予1=1
    var $showMessage;        //操作返回的提示
    var $pageCode = 1;        //当前页,程序默认为1
    var $pageSize = 10;        //每页显示记录数,程序默认为10
    var $pageUrl = '?';        //分页时传入的其它保留参数
    var $pageViewText;        //输出分页字符串
    var $pageView = false;        //是否显示分页,默认为不显示
    var $bodyAry = Array();        //返回查询的数据
    var $siteCode = Array(        //返回提示的文字,目的:多语言
        0 => '数据库连接成功!',
        1 => '数据库连接失败!',
        2 => '数据写入成功!',
        3 => '数据更新成功!',
        4 => '数据删除成功!',
        5 => '数据查询失败!',
        6 => '首页',
        7 => '上一页',
        8 => '下一页',
        9 => '尾页'
    );

      //数据库连接
    function conn(){
        try {
            $this->conn = new com("ADODB.Connection");
            $this->conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->dbPath));
            $this->showMessage = $this->siteCode[0];
        }
        catch(Exception $e){
            $this->showMessage = $e->getMessage() . '<br />' . $this->siteCode[1];
        }
    }

      //数据库关闭
    function conn_close(){
        $this->conn->close();
    }

      //写入数据
    function setData(){
        $this->conn();
        $this->conn->execute("insert into $this->tabName ($this->aryChar) values($this->aryText)");
        $this->showMessage = $this->siteCode[2];
        $this->conn_close();
    }

      //更新数据
    function upData(){
        $this->conn();
        $this->conn->execute("update $this->tabName set $this->aryChar where $this->aryText");
        $this->showMessage = $this->siteCode[3];
        $this->conn_close();
    }

      //删除数据
    function delData(){
        $this->conn();
        $this->conn->execute("delete from $this->tabName where $this->aryText");
        $this->showMessage = $this->siteCode[4];
        $this->conn_close();
    }

      //查询数据
    function getData(){
        $this->conn();
        $rs = $this->conn->execute("select $this->aryChar from $this->tabName where $this->aryText");
        if(!$rs->Eof){
            $j = 0;
            $k = 0;
            if(!preg_match("/^\d+$/" , $this->pageCode)){
                $this->pageCode = 1;
            }
            while(!$rs->Eof){
                $j ++;
                 //把记录写进当前需要显示的二维数组中
                if(($j > ($this->pageCode - 1) * $this->pageSize) && ($j <= $this->pageCode * $this->pageSize)){
                    for($i = 0 ; $i < $rs->Fields->count ; $i ++){
                        $this->bodyAry[$k][$i] = $rs->Fields[$i]->value;
                    }
                    $k ++;
                }
                $rs->movenext();
            }
            //分页
            if($this->pageView == true){
                $this->pageViewText = '[' . $j . '][' . $this->pageCode . '/' . ceil($j / $this->pageSize) . ']&nbsp;';
                if($j > $this->pageSize){
                    if($this->pageCode > 1){
                        $this->pageViewText .= "<a href='" . $this->pageUrl . "page=1'>" . $this->siteCode[6] . "</a>&nbsp;";
                        $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ($this->pageCode - 1) . "'>" . $this->siteCode[7] . "</a>&nbsp;";
                    }
                    else {
                        $this->pageViewText .= $this->siteCode[6] . "&nbsp;";
                        $this->pageViewText .= $this->siteCode[7] . "&nbsp;";;
                    }
                    if($this->pageCode < ceil($j / $this->pageSize)){
                        $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ($this->pageCode + 1) . "'>" . $this->siteCode[8] . "</a>&nbsp;";
                        $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ceil($j / $this->pageSize) . "'>" . $this->siteCode[9] . "</a>";
                    }
                    else {
                        $this->pageViewText .= $this->siteCode[8] . "&nbsp;";
                        $this->pageViewText .= $this->siteCode[9];
                    }
                }
            }
        }
        else {
            $this->showMessage = $this->siteCode[5];
            exit();
        }
        $rs->close();
        $this->conn_close();
    }
}
?>

  
  
  使用方法
  <?
  类名:mdbClass;
    作用:操作access的DB类;
    作者:biyuan(6010707);
    使用方法及范例:
        $db = new mdbClass();    //实例
        $db->tabName = "gbook";    //指定要操作的表名

        //数据写入操作
        $db->aryChar = "g_name , g_mail , g_oicq , g_tel , g_img , g_body";
        $db->aryText = "'admin' , 'xxxx@163.com' , 6010707 , '15994275xxx' , '/face/1.gif' , '测试数据'";
        $db->setData();

        //数据更新操作
        $db->aryChar = "g_name = 'scriptcn' , g_tel = '13800138xxx'";
        $db->aryText = "1 = 1";        //条件;赋予1=1表示批量操作
        $db->upData();

        //数据删除操作
        $db->aryText = "id = 4";
        $db->delData();

        //数据查询操作
        $db->aryChar = "id , g_name , g_mail , g_oicq , g_time , g_body";    //可使用&#8220;*&#8221;全部查询
        $db->aryText = "1 = 1 order by id";    //条件1 = 1表示查询所有记录
        $db->pageView = true;            //是否分页
        $db->pageSize = 10;            //指定每页记录数
        $db->pageUrl = "?id=1&";        //传入其它保留参数
        $db->pageCode = $_GET['page'];        //获取当前页序号
        $db->getData();
        echo "<table border='1'>\n";
        for($i = 0 ; $i < count($db->bodyAry) ; $i ++){    //注意这里,$db->bodyAry是一个二维数组,行 -> 列
            echo "<tr>\n";
            for($j = 0 ; $j < count($db->bodyAry[$i]) ; $j ++){
                echo "<td>" . $db->bodyAry[$i][$j] . "</td>\n";
            }
            echo "</tr>\n";
        }
        if($db->pageView == true){
            echo "<tr>\n<td colspan='7'>";
            echo $db->pageViewText;        //这里是DB类返回的分页字符
            echo "</td>\n</tr>\n</table>";
        }

        //echo $db->showMessage;    //返回提示内容,调试时可开启
  ?>

运维网声明 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-105260-1-1.html 上篇帖子: [转]php与java通用AES加密解密算法 下篇帖子: php mysqli 查询语句返回值类型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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