将公共代码存储在单独的文件中

时间:2013-10-16 16:27:01

标签: php

我正在尝试在bindColumn文件中保留separate语句和其他代码的长列表,并在需要的地方使用它们,而不是让它们污染主代码。我创建了一个名为stock.php的文件,并将所有bindColumn语句放在其中,并将它们包含在每个页面中。不知何故,我似乎没有做到这一点,并且最终会遇到很多错误。

我该怎么办?

select x, y, z, a, b, c from table;
$stmt->execute($array);
//Instead of writing all those bindColumn statements here after the select, 
//I'd like to keep them in a seperate file and include them here. How?


//In a seperate file
function bindCols(){
    $x = '$stmt->bindColumn('x',$x);';
    $x = '$stmt->bindColumn('y',$y);';
    $x = '$stmt->bindColumn('z',$z);';
    $x = '$stmt->bindColumn('a',$a);';
    $x = '$stmt->bindColumn('b',$b);';
    $x = '$stmt->bindColumn('c',$c);';
    $x = '$stmt->bindColumn('d',$d);';

    return $x;
}

1 个答案:

答案 0 :(得分:3)

有了更多信息,我可以让这个例子更完整......

   $params['calories'] = 150;
   $params['colour'] = 'red';


   $stmt = $dbh->prepare('SELECT name, colour, calories
                          FROM fruit
                          WHERE calories < :calories AND colour = :colour'); 

   bindColumns($stmt,$params); 

   $stmt->execute();

   function bindColumns(&$stmt, $params)
   {
      foreach($params as $key => $value)
      {
         $stmt->bindValue(":$key", $value); 
      } 
   }

快速注意:
* bindParam()需要通过引用获取其值 * bindValue()


好吧......结束之后:

   $params = array('name','color','calories');

   $stmt = $dbh->prepare('SELECT name, color, calories FROM fruit');  

   $stmt->execute();   

   foreach($params as $index => $name) {  
      $stmt->bindColumn("$name", $$name);  
   }   

   while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
      print $name . "\t" . $color . "\t\t" . $calories . "<br/>"; 
   }

这使用称为variable variable的东西来制作动态变量名称($$name)......就像extract()函数一样。问题是......如果我将foreach($params...放入它自己的函数中,它们就会在该函数的范围内丢失。唯一的选择是使它们成为一个数组,并通过引用传递或返回它。如果你这样做,你会使用print $return_params['name']或其他东西。

   $stmt = $dbh->prepare('SELECT name, color, calories FROM fruit');  

   $success = $stmt->execute();   

   $return = bindColumnsAfterSelect($stmt, $params); 

   while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
      print $return['name'] . "\t" . $return['color'] . "\t\t" . $return['calories'] . "<br/>"; 
   }


   function bindColumnsAfterSelect(&$stmt, $params)
   {     
      $return = array(); 

      foreach($params as $index => $name) {  
         $stmt->bindColumn("$name", $return[$name]);  
      }  

      return $return; 
   }

而且......你不需要让所有名字都相同......你可以这样做:

   $params = array('name'=>'fruit_name','color'=>'fruit_color','calories'=>'fruit_calories');

然后......

   foreach($params as $table_name => $variable_name) {  
       $stmt->bindColumn("$table_name", $$variable_name);  
   }  

Annnnnnd ......我并不真正使用PDO,所以我不确定你在做什么,但this SO post对你来说可能更容易?不知道。这是该页面的代码示例:

while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
    extract($row);
    echo $name;
    // etc
}

再次编辑:这就是你想要的......但我真的,真的,真的不会这样做。

   $stmt = $dbh->prepare('SELECT name, color, calories FROM fruit');  

   $success = $stmt->execute();   

   $return = bindColumnsAfterSelect(); 

   eval($return);  // this is a bad function to use like this.  
                   // it does what you want but... it's just not right
                   // it's extremely powerful and shouldn't be used
                   // for things like this... 

   while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
      print $name . "\t" . $color . "\t\t" . $calories . "<br/>"; 
   }

   function bindColumnsAfterSelect()
   {     
      $x  = '$stmt->bindColumn("name", $name);';
      $x .= '$stmt->bindColumn("color", $color);';
      $x .= '$stmt->bindColumn("calories", $calories);'; 

      return $x; 
   }