相思资源网 Design By www.200059.com

复制代码 代码如下:
Base dao class illustrating the usefulness of closures.
* Handles opening and closing of connections.
* Adds slashes sql
* Type checking of sql parameters and casts as appropriate
* Provides hook for processing of result set and emitting one or more objects.
* Provides hook for accessing underlying link and result objects.

<"userName","root");
define("password","root");
define("dbName","ahcdb");
define("hostName","localhost");

class BaseDao {

    function getConnection()    {
        $link = mysql_connect(hostName, userName, password);
        if (!$link)
            die("Could not connect: " . mysql_error());
        if (!mysql_select_db(dbName))
            die("Could not select database: " . mysql_error());
        return $link;
    }
   
    function setParams(& $sql, $params)    {
        if($params != null)
            $sql = vsprintf($sql, array_map(function($n) {
                if(is_int($n))
                    return (int)$n;
                if(is_float($n))
                    return (float)$n;
                if(is_string($n))
                    return "'".mysql_real_escape_string($n)."'";
                return mysql_real_escape_string($n);
            }, $params));
    }

    function executeQuery($sql, $params, $callback = null)    {
        $link  = $this->getConnection();
        $this->setParams($sql, $params);
        $return = null;
        if(($result = mysql_query($sql, $link)) != null)
            if($callback != null)
                $return = $callback($result, $link);
        if($link != null)
            mysql_close($link);
        if(!$result)
            die("Fatal Error: Invalid query '$sql' : " . mysql_error());
        return $return;
    }
 
    function getList($sql, $params, $callback)    {
        return $this->executeQuery($sql, $params, function($result, $link) use ($callback) {
            $idx = 0;
            $list = array();
            while ($row = mysql_fetch_assoc($result))
                if($callback != null)
                    $list[$idx] = $callback($idx++, $row);
            return $list;
        });
    }
   
    function getSingle($sql, $params, $callback)    {
        return $this->executeQuery($sql, $params, function($result, $link) use ($callback) {
            if ($row = mysql_fetch_assoc($result))
                $obj = $callback($row);
            return $obj;
        });
    }
}

class Example    {
    var $id;
    var $name;
   
    function Example($id, $name){
        $this->id = $id;
        $this->name = $name;
    }
   
    function setId($id){
        $this->id = $id;
    }
}

class ExampleDao extends BaseDao    {
   
   
    function getAll(){
        return parent::getList("select * from nodes", null, function($idx, $row) {
            return new Example($row["id"], $row["name"]);
        });
    }
   
    function load($id){
        return parent::getSingle("select * from nodes where id = %1\$s", array($id), function($row) {
            return new Example($row["id"], $row["name"]);
        });
    }
   
    function update($example){
        return parent::executeQuery("update nodes set name = '' where  id = -1", null, function($result, $link){
            return $result;
        });
    }
   
    function insert(& $example){
        return parent::executeQuery("insert into nodes", null, function($result, $link) use ($example){
            $id = mysql_insert_id($link);
            $example->setId($id);
            return $result;
        });
    }   
}

$exampleDao = new ExampleDao();

$list = $exampleDao->getAll());

$exampleObject = $exampleDao->load(1));

$exampleDao->update($exampleObject);

?>

标签:
PHP,匿名函数,操作数据库

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“PHP中使用匿名函数操作数据库的例子”

暂无PHP中使用匿名函数操作数据库的例子的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。