I mainly use this handy function to handle db strings (close your eyes to PDO and / or other things)
function fetch($query,$func) { $query = mysql_query($query); while($r = mysql_fetch_assoc($query)) { $func($r); } }
With this function, I can simply do:
fetch("SELECT title FROM tbl", function($r){
Let's say now I need to combine all $r['title'] into var (this is just an example).
How could I do this? I thought something like this, but it is not very elegant:
$result = ''; fetch("SELECT title FROM tbl", function($r){ global $result; $result .= $r['title']; }); echo $result;
scope closures php
dynamic Dec 06 '11 at 17:11 2011-12-06 17:11
source share