欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中使用匿名函数操作数据库的例子

程序员文章站 2022-06-04 11:43:39
复制代码 代码如下: base dao class illustrating the usefulness of closures. * handles opening...

复制代码 代码如下:

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.

<?php

define("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);

?>