欢迎光临
我们一直在努力

国外好东西真的多,现在贴上一个访问ACCESS的类!-PHP教程,PHP应用

建站超值云服务器,限时71元/月

这是access的类

<?
class accessdbm
{
    var $count = 0;
    var $values = array();
    var $file = "";
    var $error = "";
    var $exists = false;
    var $static = false;
    var $exact = false;
    var $dbm;

//    older version of php cant do the new classname(args)
//    use initilize() if this is the case.

//    *******************************************************

    function accessdbm ($dbmfile, $static = 0)
    {
        global $php_errormsg;

        if(!empty($dbmfile))
        {
            if(file_exists($dbmfile))
            {
                $this->exists = true;
            }
            if($static != 0)
            {
                $this->static = true;
            }
            $this->file = $dbmfile;
        }
        return;
    }

//    *******************************************************

//    identical to accessdbm
    function initialize ($dbmfile, $static = 0)
    {
        global $php_errormsg;

        if(!empty($dbmfile))
        {
            if(file_exists($dbmfile))
            {
                $this->exists = true;
            }
            if($static != 0)
            {
                $this->static = true;
            }
            $this->file = $dbmfile;
        }
        return;
    }

//    *******************************************************

    function add_entry ($key, $val)
    {
        $results = 0;
        $dbm = $this->open_dbm();
        if(!$dbm) { return false; }

        if(!(dbmreplace($dbm,$key,$val)))
        {
            if(!(dbmexists($dbm,$key)))
            {
                $this->error = "fatal error : could not replace $key with $val";
                $this->close_dbm($dbm);
                return false;
            }
        }
        $this->close_dbm($dbm);
        return true;        
    }

//    *******************************************************

    function remove_entry ($key)
    {
        global $php_errormsg;
        $removed = false;

        $dbm = $this->open_dbm();
        if(!$dbm) { return false; }

        if(dbmexists($dbm,$key))
        {
            if(!dbmdelete($dbm,$key))
            {
                if(dbmexists($dbm,$key))
                {
                    $this->error = "unable to remove [$key] : [$php_errormsg]";
                    $this->close_dbm($dbm);
                    return false;
                }
            }
            else
            {
                $this->close_dbm($dbm);
                $removed = true;
            }
        }
        else
        {
            $this->error = "key [$key] does not exist";
            $this->close_dbm($dbm);
            return false;
        }
        return true;
    }

//    *******************************************************

    function get_value ($key)
    {
        $val = "";
        $readonly = true;

        $dbm = $this->open_dbm($readonly);

        if(!$dbm) { return false; }

        if(dbmexists($dbm,$key))
        {
            $val = dbmfetch($dbm,$key);
        }
        $this->close_dbm($dbm);
        return $val;
    }

//    *******************************************************

    function open_dbm ($readonly = false)
    {
        global $php_errormsg;

        if($this->static)
        {
            if(!(empty($this->dbm)))
            {
                $dbm = $this->dbm;
                return ($dbm);
            }
        }

        $filename = $this->file;

        if(!$this->exists)
        {
            $dbm = @dbmopen($filename,"n");
        }
        else
        {
            if(!$readonly)
            {
                // we want the warning here if we cant be
                // a writer
                $dbm = dbmopen($filename,"w");
            }
            else
            {
                $dbm = @dbmopen($filename,"r");
            }
        }
        if( (!$dbm) or (empty($dbm)) )
        {
            $this->exists = false;
            $this->static = false;
            $this->error = "unable to open [$filename] [$php_errormsg]";
            return false;
        }
        $this->exists = true;
        if($this->static)
        {
            $this->dbm = $dbm;
        }

        return ($dbm);

    }

//    *******************************************************

    function find_key ($search)
    {
        $val = "";

        $dbm = $this->open_dbm(1);
        if(!$dbm) { return false; }
        if(dbmexists($dbm,$search))
        {
            // wow an exact match
            $val = dbmfetch($dbm,$search);
            $this->close_dbm($dbm);
            $this->exact = true;
            return $val;
        }
        else
        {
            $this->exact = false;
            $key = dbmfirstkey($dbm);
            while ($key)
            {
                // strip the first whitespace char and
                // everything after it.
                $test = ereg_replace(" .*","",$key);
                if(eregi("^$test",$search))
                {
                    $val = dbmfetch($dbm,$key);
                    $this->close_dbm($dbm);
                    error_log("test [$test] matched [$search]",0);
                    return $val;
                }
                $key = dbmnextkey($dbm,$key);
            }
        }
        // didnt find it
        $this->close_dbm($dbm);
        return false;
    }

//    *******************************************************

    // returns the key
    function find_val ($search)
    {
        $this->exact = false;

        $dbase = $this->get_all();
        if(empty($dbase))
        {
            error_log("error dbase is empty $db->error",0);
            return false;
        }
        while ( list ( $key, $val ) = each ($dbase) )
        {
            if($search == $val)
            {
                $this->exact=true;
                return $key;
            }
            else
            {
                // strip the first whitespace char and
                // everything after it.

                $test = ereg_replace(" .*","",$val);

                if(eregi("^$test",$search))
                {
                    $this->exact = false;
                    return $key;
                }
            }
        }
        // didnt find it
        return false;
    }

//    *******************************************************

    function get_all ()
    {
        $values = array();
        $count = 0;
        $readonly = true;
        $dbm = $this->open_dbm($readonly);
        if(!$dbm) { return false; }

        $key = dbmfirstkey($dbm);

        while ($key)
        {
            $val = dbmfetch($dbm,$key);
            $values[$key] = $val;
            $count++;
            $key = dbmnextkey($dbm, $key);
        }
        $this->count = $count;
        $this->values = $values;
        $this->close_dbm($dbm);
        return $values;
    }

//    *******************************************************

    function close_dbm ($dbm)
    {
        $results = false;

        if(!$this->static)
        {
            $results = dbmclose($dbm);
        }
        return $results;
    }

//    *******************************************************

    function static_close()
    {
        $results = false;

        if(!$this->dbm)
        {
            $this->error = "no static dbm to close";
            return false;
        }
        $dbm = $this->dbm;
        $results = dbmclose($dbm);
        unset($this->dbm);
        return $results;
    }

//    *******************************************************

}
?>

这个连接上!
include("class.accessdbm.php3");
    $static = true;
    $dbase = new accessdbm("/path/to/file.dbm",$static);

    register_shutdown_function($dbase->static_close());

    if(!$dbase->add_entry("cdi","cdi@thewebmasters.net))
    {
        echo "error adding entry: $dbase->error\n";
    }
    $values = $dbase->get_all()
    while ( list ($key,$val) = each ($values) )
    {
        echo "key: $key  val: $val \n";
    }
    exit;

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 国外好东西真的多,现在贴上一个访问ACCESS的类!-PHP教程,PHP应用
分享到: 更多 (0)