当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-26 17:18:18 浏览: 评论:0 

这篇文章主要介绍了PHP中使用匿名函数操作数据库的例子,本文直接给出类的代码、使用例子、继承例子,需要的朋友可以参考下,代码如下:

  1. Base dao class illustrating the usefulness of closures. 
  2. * Handles opening and closing of connections. 
  3. * Adds slashes sql 
  4. * Type checking of sql parameters and casts as appropriate 
  5. * Provides hook for processing of result set and emitting one or more objects. 
  6. * Provides hook for accessing underlying link and result objects. 
  7. <?php 
  8.  
  9. define("userName","root"); 
  10. define("password","root"); 
  11. define("dbName","ahcdb"); 
  12. define("hostName","localhost"); 
  13.  
  14. class BaseDao { 
  15.  
  16.     function getConnection()    { 
  17.         $link = mysql_connect(hostName, userName, password); 
  18.         if (!$link) 
  19.             die("Could not connect: " . mysql_error()); 
  20.         if (!mysql_select_db(dbName)) 
  21.             die("Could not select database: " . mysql_error()); 
  22.         return $link; 
  23.     } 
  24.     
  25.     function setParams(& $sql, $params)    { 
  26.         if($params != null) 
  27.             $sql = vsprintf($sql, array_map(function($n) { 
  28.                 if(is_int($n)) 
  29.                     return (int)$n; 
  30.                 if(is_float($n)) 
  31.                     return (float)$n; 
  32.                 if(is_string($n)) 
  33.                     return "'".mysql_real_escape_string($n)."'"; 
  34.                 return mysql_real_escape_string($n); 
  35.             }, $params)); 
  36.     } 
  37.  
  38.     function executeQuery($sql, $params, $callback = null)    { 
  39.         $link  = $this->getConnection(); 
  40.         $this->setParams($sql, $params); 
  41.         $return = null; 
  42.         if(($result = mysql_query($sql, $link)) != null) 
  43.             if($callback != null) 
  44.                 $return = $callback($result, $link); 
  45.         if($link != null) 
  46.             mysql_close($link); 
  47.         if(!$result) 
  48.             die("Fatal Error: Invalid query '$sql' : " . mysql_error()); 
  49.         return $return; 
  50.     } 
  51.  
  52.     function getList($sql, $params, $callback)    { 
  53.         return $this->executeQuery($sql, $params, function($result, $link) use ($callback) { 
  54.             $idx = 0; 
  55.             $list = array(); 
  56.             while ($row = mysql_fetch_assoc($result)) 
  57.                 if($callback != null) 
  58.                     $list[$idx] = $callback($idx++, $row); 
  59.             return $list; 
  60.         }); 
  61.     } 
  62.     
  63.     function getSingle($sql, $params, $callback)    { 
  64.         return $this->executeQuery($sql, $params, function($result, $link) use ($callback) { 
  65.             if ($row = mysql_fetch_assoc($result)) 
  66.                 $obj = $callback($row); 
  67.             return $obj; 
  68.         }); 
  69.     } 
  70. } 
  71.  
  72. class Example    { 
  73.     var $id; 
  74.     var $name; 
  75.     
  76.     function Example($id, $name){ 
  77.         $this->id = $id; 
  78.         $this->name = $name; 
  79.     } 
  80.     
  81.     function setId($id){ 
  82.         $this->id = $id; 
  83.     } 
  84. } 
  85.  
  86. class ExampleDao extends BaseDao    { 
  87.     
  88.     
  89.     function getAll(){ 
  90.         return parent::getList("select * from nodes", null, function($idx, $row) { 
  91.             return new Example($row["id"], $row["name"]); 
  92.         }); 
  93.     } 
  94.     
  95.     function load($id){ 
  96.         return parent::getSingle("select * from nodes where id = %1\$s", array($id), function($row) { 
  97.             return new Example($row["id"], $row["name"]); 
  98.         }); 
  99.     } 
  100.     
  101.     function update($example){ 
  102.         return parent::executeQuery("update nodes set name = '' where  id = -1", null, function($result, $link){ 
  103.             return $result; 
  104.         }); 
  105.     } 
  106.     
  107.     function insert(& $example){ 
  108.         return parent::executeQuery("insert into nodes", null, function($result, $link) use ($example){ 
  109.             $id = mysql_insert_id($link); 
  110.             $example->setId($id); 
  111.             return $result; 
  112.         }); 
  113.     }    
  114. } 
  115.  
  116. $exampleDao = new ExampleDao(); 
  117.  
  118. $list = $exampleDao->getAll()); 
  119.  
  120. $exampleObject = $exampleDao->load(1)); 
  121.  
  122. $exampleDao->update($exampleObject); 
  123.  
  124. ?> 

Tags: PHP匿名函数

分享到: