2013-04-08 49 views
0

出於某種原因,我無法檢索我想要的出價信息。我希望它顯示當前項目的所有出價。 db,query或php有問題嗎? 其他功能工作,只是bid[bidprice]沒有。當我設法得到它,以顯示它不會顯示等Mysql問題 - 功能不起作用

/* 
============ 
db class 
============ 
*/ 

public function allbids($id){ 
     $qry = "SELECT userName , bidPrice as bidPrice FROM bids , users WHERE productID =    $id"; 
     $rs = $this -> db -> query($qry); 
     if($rs) { 
      if($rs ->num_rows > 0) { 
       $bid = $rs -> fetch_assoc(); 

      } 
      return $bid; 

     } else { 
      echo 'Error Executing Query'; 
     } 
     return false; 
    } 


/* 
============ 
html php 
============ 
*/ 
<?php 

class ProductView extends View { 
    protected function displayContent() { 
     if(isset($_GET['id'])) { 
     //get the record from database 
      $this -> product = $this -> model -> getProductByID($_GET['id']); 
      $this -> bidprice = $this -> model ->allbids($_GET['id']); 
       if(is_array($this -> product)) { 
        $html = $this -> displayProduct(); 
        } else { 

         $html .= '<p>Sorry, that product is not available</p>'; 

        }    
         } else { 
         header("Location:index.php?page=error"); 

     } 
      return $html; 
    } 


    private function displayProduct() { 

      $html = '<div id="product">'; 
      $html .= '<img src="images/products/'.$this -> product['productImage'].'" alt="'.$this -> product['productName'].'" />'; 
      $html .= '<h3>'.$this -> product['productName'].'</h3>'; 
      $html .= '<p><strong>$'.$this -> product['productPrice'].'.00'.'</strong></p>'; 
      //.sprintf("%.2f" result was breaking the query placed .00. to give it currency rate. 
      $html .= '<p>'.$this -> product['productDescription'].'</p>'; 
      $html .= '<p>'.$this -> bidprice['bidprice'].'</p>'; 
       foreach($this -> bidprice as $val) 
     { 
      $html .= '<p> Bid Price'.$val['bidPrice'].'</p>'; 
      $html .= '<p> Bidded By'.$val['userName'].'</p>'; 
     } 

      $html .= '</div>'; 
      $html .='<div id="space">'; 
      $html .='</div>'; 

     return $html;   
    }  
} 
+0

你得到的錯誤是什麼 – PSR 2013-04-08 10:35:08

+0

它不會給出任何錯誤? – 2013-04-08 10:35:17

+1

通常,當我面臨這個問題時,我複製$ qry與示例$ id(在這種情況下),並嘗試從mysqlworkbench或類似的運行它,這樣你可以放棄,如果它的PHP或MySQL的不好 – aleation 2013-04-08 10:35:22

回答

0

必須更新您的查詢指定從中領域應採取的表:

SELECT `users`.`userName` AS `userName`, 
     `bids`.`bidPrice` AS `bidPrice` 
FROM `bids`, `users` 
WHERE `bids`.`productID` = $id 

針對上述情況,我已假定productID字段包含在bids表中。此外,你只有從該集合中獲取第一個返回的記錄。如果您想獲取整個出價歷史記錄,則需要使用while()循環,然後將每條記錄寫入bids數組,然後將其返回。

像這樣的事情應該達到你想要做什麼:

public function allbids($id) 
{ 
    $qry = "SELECT `users`.`userName` AS `userName`, 
      `bids`.`bidPrice` AS `bidPrice` 
      FROM `bids`, `users` 
      WHERE `bids`.`productID` = $id"; 

    $rs = $this->db->query($qry); 
    if($rs) 
    { 
     $bids = array(); 
     while($bid = $rs->fetch_assoc()) 
     { 
      $bids = $bid 
     } 
     return $bids; 
    } 
    else 
    { 
     echo 'Error Executing Query'; 
     return false; 
    } 
} 

調用allbids(1)將返回的競價記錄數組的產品ID 1,或false如果有與查詢錯誤。

+0

這是它產生的答案,但不是出價 – 2013-04-08 10:37:18

+0

對不起?那個評論對我來說並不意味着什麼? – BenM 2013-04-08 10:37:42

+0

投標Pricea 出價的BYA 投標價格1 出價的BY1 – 2013-04-08 10:38:05