2011-02-01 108 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

相关问题