首页 >

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

后端开发|php教程PHP中使用匿名函数操作数据库的例子【PHP】
PHP,匿名函数,操作数据库
后端开发-php教程
百度地图 php 源码,ubuntu升级不能启动,时光网爬虫代码,php mapper,新版seo优化lzw
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.
2016钓鱼网站源码,vscode快速查找关键字,ubuntu滚轮插件,tomcat关闭服务自启动,爬虫做成word,易语言写php,邵武一般seo哪家好lzw
<?php
刷下拉 源码,禅道vscode,ubuntu延迟,tomcat安装介绍,sqlite升级的语句,select2插件,编写前端框架那个软件最好,墙上爬虫是什么虫,php iis xp,南昌seo优化企业,易语言连接网站数据库连接,网页源码解析器,淘宝店免费全套模板下载lzw
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中使用匿名函数操作数据库的例子【PHP】
  • 用C语言操作MySQL数据库的通用方法【MySQL】
  • 用C语言操作MySQL数据库的通用方法【MySQL】 | 用C语言操作MySQL数据库的通用方法【MySQL】 ...

    PHP中使用匿名函数操作数据库的例子【PHP】
  • django mysql数据库连接 |mysql count over
  • django mysql数据库连接 |mysql count over | django mysql数据库连接 |mysql count over ...

    PHP中使用匿名函数操作数据库的例子【PHP】
  • 跟老齐学Python之使用Python操作数据库(1)
  • 跟老齐学Python之使用Python操作数据库(1) | 跟老齐学Python之使用Python操作数据库(1) ...