将列/变量添加到PDO FetchAll行

时间:2012-02-24 17:28:26

标签: php mysql database object pdo

我是使用PDO的新手并且有点卡住了 - 有没有办法可以在返回的fetchAll数组中添加一行?

我想在行中使用一个值并从中创建一个新值。我希望我的榜样足够清楚 -

 class.php
--------------------
class connect
{
//all PDO database connection functions here
}

class Display
{
   function cars($colour)
   {
   $crud = new crud();
   $crud->conn();
   $sql = "SELECT * FROM cars WHERE colour=:colour";
   $stmt = $crud->db->prepare($sql);
   $stmt->bindParam(':colour', $colour);
   $stmt->execute();
   $rows =  $stmt->fetchAll(PDO::FETCH_ASSOC);

 ****WHAT I WANT TO DO (see edit)*****
   foreach ($rows as $row) 
       {
          ADD A ROW CALLED 'newprice' that equals ($row['oldprice'] + 300 / 2)
       }


       $this->results = $rows;
    }
 }


 index.php
--------------------
//other html code...

$D = new Display;
$D->cars('1');
foreach($D->results as $row)
{
?>          

<div class="car">
    <h1>Car Name:</h2>
    <?=($row['model']);?>
    Old Price:
    <?=$row['price'];?>
    New Price:
    <?=($row['newprice']);?>    <- outputting the new row I created
 </div>
}?> 

可以这样做,还是有更有效的方式来做到这一点?

对不起,我想我的例子并不是很清楚!我想要做的不仅仅是在数学上改变新列的值 - 我想preg_split /通过另一个类等运行它!

到目前为止,我得到的最佳解决方案是从PDO提供的数组创建一个新的多维数组,然后添加/编辑该数组,如下所示:

     $new=array();
     $key_loop = 0;
     $row_loop = 0;

      foreach ( $rows as $val )
      {
       $keys = array_keys($val);
       foreach ($keys as $key)
         {                
         $new[$row_loop][$keys[$key_loop]] = $val[$keys[$key_loop]];      
         $new[$row_loop]['new_entry'] = 'this works';
         $key_loop++;
         }       
         $row_loop++; 
      $key_loop = 0;
      }

4 个答案:

答案 0 :(得分:2)

所以,这段代码是我头脑中的快速记录(即没有保证),但我认为这样的事情对你有用。

...

foreach ($rows as $row) 
{
   // Get old price
   $old_price = $row['oldprice'];

   // Do some stuff with old price
   $new_price = changeOldPrice($old_price);


   // Insert new price into same row, under the newprice column
   $sql = "UPDATE cars SET newprice=? WHERE id=?";
   $stmt = $crud->db->prepare($sql);
   $stmt->execute( array(':newprice' => $new_price, ':id' => $row['id']) );
}

...

public function changeOldPrice($old_price)
{ 
    // Some random regular expression split
    $new_price = preg_split("[,]+", $old_price) 

    // Pass to random class
    $myClass = new MyClass;
    $new_price = $myClass->doSomethingCool($new_price);

    // Return
    return $new_price;
}

现在是坏消息,这是一个很多的UPDATES正在运行,并将严重降低性能。解决方案是使用transactions一次编写所有这些内容。

<强> [编辑] 为了响应您的编辑,请参阅更新的代码。

答案 1 :(得分:1)

也许就是这样。

SELECT *, (oldprice +300 / 2) as newprice FROM cars WHERE colour=:colour

答案 2 :(得分:1)

我最终做的是循环遍历行并将它们粘贴在一个新的多维数组中,并使用我需要的额外值 - 可能不是最优雅的方式,但它对我有用:

$new  = array();
$array_loop = 0;

$sql = "select old_price, item_id from car";

...

$stmt->execute();
$rows =  $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $val)
{
    $new[$array_loop]['item_id']   = $val['item_id'];
    $new[$array_loop]['old_price'] = $val['old_price'];

    $newprice = $val['old_price'] + 300 / 2;
    $new[$array_loop]['new_price'] = $new_price;
    $array_loop++;
}

答案 3 :(得分:0)

如果有人遇到同样的问题(超过600次观看),这就是解决方案:

而不是在结果中循环:

  tree mytree = (tree)malloc(sizeof(treeNode));
  mytree->key = 17;
  mytree->left = (tree)malloc(sizeof(treeNode));
  mytree->left->key = 5;
  mytree->left->left = (tree)malloc(sizeof(treeNode));
  mytree->left->right = (tree)malloc(sizeof(treeNode));
  mytree->left->left->key = 20;
  mytree->left->right->key = 2;
  mytree->right = (tree)malloc(sizeof(treeNode));
  mytree->right->key = 1;
  mytree->right->left = (tree)malloc(sizeof(treeNode));
  mytree->right->right = (tree)malloc(sizeof(treeNode));
  mytree->right->left->key = 6;
  mytree->right->right->key = 3;

使用

foreach($D->results as $row)

并使用$ key引用而不是$ rows

来操作$ results