【发布时间】:2011-02-01 16:19:41
【问题描述】:
我首先查询数据库以获取与某个用户 ID 关联的所有记录,然后我需要进入并修改数组,因为其中一个字段是一个 ID,我需要与该 ID 关联的名称。
所以我使用 columnCount 遍历 id 索引处的结果数组,并将其替换为正确的名称,这对于前六个结果来说效果很好。 columnCount 仅返回 6,但前六个已按应有的方式重命名。但除此之外,它会从该 pdostatement 中获取结果并正常填充表,其中包含所有相关数据,现在 17 行。
为什么它返回 6,或者我在做什么来得到错误的列数?
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";
}
【问题讨论】: