如何在PHP中利用匿名函數(shù)操作數(shù)據(jù)庫(kù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)長(zhǎng)葛免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。具體方法如下
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); ?>
關(guān)于如何在PHP中利用匿名函數(shù)操作數(shù)據(jù)庫(kù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
名稱欄目:如何在PHP中利用匿名函數(shù)操作數(shù)據(jù)庫(kù)-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://muchs.cn/article42/sppec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、品牌網(wǎng)站制作、域名注冊(cè)、云服務(wù)器、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容