Skip to main content
<?     function bq($a$preview false){         # Version 1.1         # Copyright Jonas Eklundh Communication AB 2008         if (empty($a["from"]) && empty($a["table"])) return false;         if (!empty($a["from"]) && !is_array($a["from"])) $a["from"] = preg_split("/,s+/"$a["from"]);         if (!empty($a["select"]) && !is_array($a["select"])){             preg_match_all('#s*((?:([^)]+)|[^,])+)s*#s'$a["select"], $m);             $a["select"] = $m[1];         }         if (!empty($a["select"])){             $calc = isset($a["calculate"]) && $a["calculate"] == "yes" "SQL_CALC_FOUND_ROWS " false;             $query[] = "select $calcn  " join(",n  "array_map("trim"$a["select"]));         } elseif (!empty($a["command"])) {             $query[] = $a["command"];         } else {             return false;         }         if (!empty($a["from"])){             $query[] = "fromn  " join(",n  "$a["from"]);         } elseif (!empty($a["table"])) {             $query[] = "  " $a["table"];         }         if (!empty($a["join"])){             foreach ($a["join"] as $dir => $ar){                 $as "";                 $dir is_numeric($dir) ? "" "$dir ";                 if (!empty($ar["dir"])) $dir $ar["dir"] . " ";                 if (!empty($ar["query"]) && !empty($ar["as"])){                     $ar["db"] = bq($ar["query"]);                     $as "as " $ar["as"];                 }                 $query[] = "{$dir}joinn  ($ar[db]$as on (" bq_where($ar["on"], "and") . ")";             }         }         if (!empty($a["search"])){             $col = !empty($a["collate"]) ? "COLLATE $a[collate]false;             $cond $a["searchcond"] ?? "or:search";             foreach ($a["search"] as $search => $fields){                 foreach ($fields as $k){                     #$a["where"][$cond][] = "`" . $k . "` like '%" . sqlesc($search) . "%' $col";                     $a["where"][$cond][] = $k " like '%" sqlesc($search) . "%' $col";                 }             }         }         if (!empty($a["set"])){             $query[] = "set";             $query[] = "  " bq_where($a["set"], ","true);         }         if (!empty($a["where"])){             $query[] = "where";             $query[] = "  " bq_where($a["where"], "n  and");         }         if (!empty($a["collate"])){             #$query[] = "COLLATE '$a[collate]'";         }         if (!empty($a["group"])){             $query[] = "group by " clean_keyword($a["group"]);             #if (!empty($a["collate"])) $query[] = " COLLATE '$a[collate]'";         }         if (!empty($a["having"])){             foreach ($a["having"] as $k => $v){                 $having[] = "$k = $v";             }             $query[] = "having " join(" and "$having);         }         if (!empty($a["order"])){             if (is_array($a["order"])){                 foreach ($a["order"] as $k => $v){                     if (is_numeric($k)){                         $order[] = clean_keyword($v);                     } else {                         $v clean_keyword($v);                         $k clean_keyword($k);                         $order[] = "$k $v";                     }                 }                 $query[] = "order by " join(", "$order);             } else {                 #$order = clean_keyword($a["order"]);                 $order $a["order"];                 $query[] = "order by $order";             }         }         if (!empty($a["limit"])){             $query[] = "limit " clean_keyword(sqlesc($a["limit"]));         }         $query join("n"$query);         if (!empty($preview)){             if (group("superduperadmin")){                 return notice(tag($query"pre"));             }         } else {             return $query;         }         return false;     } ?>
Note that the code is the exact code that I am using and may use functions that are exclusive to my system, and as such may not be suitable for copy/paste. Plus, these functions can change at any time and any blogs/tutorials that depend on these functioning in a specific way may have changed since it was written. If such a discrepancy is found, please email me at mr@sandman.net

If the function "lp()" is used, it's my "Language Print" function, that translates phrases from english to the site language. So, instead of "lp('and')" you would use just "and".