Jump to content
php.lv forumi

Kādām nav FP custom query funkcija?


waplet

Recommended Posts

function query($q) {
$awesominizer = array('awesome', 'innovative', 'superb', 'magical', 'splendid');
echo $awesominizer[array_rand($awesominizer)];
return mysql_query($q);
}

 

Pēc maniem uzskatiem uzlabota query funkcija. Dara tieši to pašu ko mysql_query + izvada superīgu tekstu, kas veicina produktivitāti.

Link to comment
Share on other sites

es vēl redzu sitādu variantu:

 

function db_q($q)
{
   return mysql_query($q) or die(mysql_error());
}

 

Vienīgais labums, ko saskatu šajā, tad var mainīt or die daļu. respektīvi var izķert kļūdu un rādīt/nerādīt kļūdu.

Un ja kaut ko grib pamainīt, tad nav jāmaina visās vietās.

Link to comment
Share on other sites

Var sakabināt klāt automātisku parametru eskeipošanu, aptuveni tā:

 

function q($query){
 function esc(&$arr){
   foreach($arr as &$el){
     if (is_string($el)){
       $el = "'".mysql_real_escape_string($el)."'";
     } elseif (is_array($el)){
       esc($el);
$el=implode(',',$el); 
     }
   }
 }    
 $args=func_get_args();
 array_shift($args);
 print_r($args);
 esc($args);
 print_r($args);
 $query=vsprintf($query, $args);	
 $res=mysql_query($query) or die(mysql_error().' '.$query);
 return $res;
}

 

izmanto aptuveni tā:

 

$res=q('SELECT * FROM users WHERE');
$res=q('SELECT * FROM users WHERE un=%s and psw=%s',$un,$psw);
$res=q('SELECT * FROM users WHERE un IN (%s)',array('john','bill'));

Link to comment
Share on other sites

Brīvā improvizācija par tēmu

/**
Izpilda vaicaajumu un pie katras rindas izpilda callback. 
Man naff ne jausmas kaa php kaut ko asinhroni izpildiit, taadeelj 
risinaajums principaa ir bezjeedziigs, tiiri pirkstu virinaashanai.
principaa izmanto globaalo mysql linku, kas ir patizli, bet man ir lauziens 
rakstiit visu mysql wrapperi funkcionaali, kas savukaart dotu shaadaas taadas 
priekshrociibas. Principaa uzskatu, PHP prieksh FP sachko.
 Demonstree teoreetiski shaadads funkcionaalaas
programeeshas iespeejas: closures, anoniimaas funkcijas :)
Taa kaa man naff php uz datora un ir lauziens instaleet, tad kods naff notesteets.
(php kauchkur jaavelk, jaakompileee, etc... ). lauziens.
Laidiisies tikai uz 5.3++ 
izmantoshanas piemeers vareetu buut shaads:
 super_q("SELECT * FROM TABLE", function ($data) { 
                                   echo "<tr>";
                                   $row = function($item){
                                     echo "<td>" . htmlspecialchars($item) . "</td>";
                                   }
                                   array_walk($row, $data);
                                   echo "</tr>";
                                 },
                                 function (){
                                   echo "<table>";
                                 }, function (){
                                   echo "</table>";
                                 }, function ($error, $number){
                                   ECHO "EPIC FAIL($error)[$number]"; 
                                 });
*/
function super_q($query, $onData, $onStart = NULL, $onEnd = NULL, $onFail = NULL){
$result = mysql_query($query);
if ($onFail){
   $fail = function() { 
     $onFail(mysql_error(), mysql_errno());
   }
} else {
  $fail = function() { }
}
if ($onStart){
  $start = function () { $onStart(); } 
} else {
  $start = function () { } 
}
if ($onEnd){
   $end = function () { $onEnd(); }
} else {
  $end = function() { }
}
if $result === false {
   $fail;
}
$started = false;
$walk_mysql = function (){
 while($row = mysql_fetch_assoc($result)){
  $onData($row);
 }
}
$start();
$walk_mysql();
$end();
}

 

es gan taisiitu OOP variantaa, as tad var ieimplementeet interfeisus itereeshanai etc, kas paveertu shaadas smukas iespeejas...

<?php 
foreach(new Query("SELECT * FROM TABLE") as $row){
  var_dump($row);
}
if (count(Query("SELECT count(*) FROM table")) > 0){
  echo "YO";
}
?>

Edited by krikulis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...