PHP中使用匿名函数操作数据库的例子
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);
?>
上一篇: php中in_array函数用法分析
推荐阅读
-
Oracle中 关于数据库存储过程和存储函数的使用
-
以实例全面讲解PHP中多进程编程的相关函数的使用
-
详解PHP中的mb_detect_encoding函数使用方法
-
Python中的匿名函数使用简介
-
php中curl、fsocket、file_get_content三个函数的使用比较
-
php中的filesystem文件系统函数介绍及使用示例
-
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
-
php操作redis中的hash和zset类型数据的方法和代码例子
-
PHP 删除文件与文件夹操作 unlink()与rmdir()这两个函数的使用
-
PHP中的MYSQL常用函数(php下操作数据库必备)