我首先查询数据库以获取与某个用户标识关联的所有记录,然后我需要进入并修改数组,因为其中一个字段是一个id,我需要与该id关联的名称。< / p>
所以我使用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";
}
答案 0 :(得分: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}不一致。