PHP anonymous _php tutorial for using anonymous functions to manipulate databases

Source: Internet
Author: User

Example of using anonymous functions in PHP to manipulate databases, PHP anonymous


Copy the Code code as follows:
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);

?>

http://www.bkjia.com/PHPjc/912671.html www.bkjia.com true http://www.bkjia.com/PHPjc/912671.html techarticle PHP uses anonymous functions to manipulate the database example, PHP anonymous copy code is as follows: Base DAO class illustrating the usefulness of closures. * Handles Opening and Clos ing of con ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.