加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 02:10:06 所属栏目:PHP教程 来源:网络整理
导读:《:PHP中使用匿名函数操作数据库的例子》要点: 本文介绍了:PHP中使用匿名函数操作数据库的例子,希望对您有用。如果有疑问,可以联系我们。 Base dao class illustrating the usefulness of closures. * Handles opening and closing of connections. * A

《:PHP中使用匿名函数操作数据库的例子》要点:
本文介绍了: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利用<?php

PHP利用define("userName","root");
define("password","root");
define("dbName","ahcdb");
define("hostName","localhost");

PHP利用class BaseDao {

PHP利用??? 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));
??? }

PHP利用??? 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,$callback)??? {
??????? return $this->executeQuery($sql,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,$link) use ($callback) {
??????????? if ($row = mysql_fetch_assoc($result))
??????????????? $obj = $callback($row);
??????????? return $obj;
??????? });
??? }
}

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

PHP利用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",$link){
??????????? return $result;
??????? });
??? }
???
??? function insert(& $example){
??????? return parent::executeQuery("insert into nodes",$link) use ($example){
??????????? $id = mysql_insert_id($link);
??????????? $example->setId($id);
??????????? return $result;
??????? });
??? }???
}

PHP利用$exampleDao = new ExampleDao();

PHP利用$list = $exampleDao->getAll());

PHP利用$exampleObject = $exampleDao->load(1));

PHP利用$exampleDao->update($exampleObject);

PHP利用?>

《:PHP中使用匿名函数操作数据库的例子》是否对您有启发,欢迎查看更多与《:PHP中使用匿名函数操作数据库的例子》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读