2011-02-01 49 views
1

我先查詢數據庫以獲取與特定用戶ID相關的所有記錄,然後我需要進入並修改數組,因爲其中一個字段是一個ID,我需要與該ID相關聯的名稱。爲什麼PDOStatement-> columnCount不能返回正確的數字?

因此,我使用columnCount遍歷id索引處的結果數組,並用正確的名稱替換它,這對於前六個結果來說工作正常。 columnCount只返回6,但前六個按照它們應該重新命名。但是除此之外,它會從這個pdostatement獲得結果並正常填充表格,幷包含所有相關數據,現在有17行。

爲什麼它返回6,或者我在做什麼來得到錯誤的columncount?

global $__CMS_CONN__; 
    $timeqry = 'SELECT facility_id, program, date, visit_length, mileage, served FROM timesheet_db WHERE volunteer_id = '.$_SESSION['user_id']; 
    $stmt = $__CMS_CONN__->prepare($timeqry); 
    $stmt->execute(); 
    $columns = $stmt->columnCount(); 
    print $columns; 
    if($stmt) 
    { 
     $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); 

     for($x=0;$x<$stmt->columnCount();$x++) 
     { 
      global $__CMS_CONN__; 
      $qry = 'SELECT facility FROM facility_db WHERE id = '.$arrValues[$x]['facility_id']; 
      $stmt1 = $__CMS_CONN__->prepare($qry); 
      $stmt1->execute(); 
      if($stmt1) 
      { 
       $facilityName = $stmt1->fetchAll(PDO::FETCH_ASSOC); 
       foreach ($facilityName as $item) 
       { 
        foreach ($item as $key => $val) 
        { 
         $arrValues[$x]['facility_id'] = $val; 
        } 
       } 
      } 
     } 
     print "<table style=\"font-size:90%\">\n"; 
     print "<tr>\n"; 
     print "<th style=\"width:100%\">Facility</th>"; 
     print "<th>Program</th>"; 
     print "<th>Date</th>"; 
     print "<th>Visit Length</th>"; 
     print "<th>Mileage</th>"; 
     print "<th>Served</th>"; 
     print "</tr>"; 
     foreach ($arrValues as $row) 
     { 
      print "<tr>"; 
      foreach ($row as $key => $val) 
      { 
       print "<td>$val</td>"; 
      } 
      print "</tr>\n"; 
     } 
     print "</table>\n"; 
    } 

回答

2

你問的六列在你的第一個SELECT查詢:

SELECT facility_id, program, date, visit_length, mileage, served FROM timesheet_db WHERE volunteer_id = $_SESSION['user_id'] 

因此,很自然,columnCount()是6,不按行數乘以列數退回或類似的東西那。

如果你想的數,你需要count($arrValues)(手冊上說與rowCount()數據庫的行爲是不一致的關於SELECT S)。

+0

LOL!男孩我覺得自己像一個tard。我試圖得到返回結果的數量 – Ryan 2011-02-01 16:24:25

相關問題