【发布时间】:2012-12-21 15:49:54
【问题描述】:
我目前在while loop 中运行了一个 MySQL 脚本,如下所示:
$Query = mysql_query("SELECT * FROM appointments");
while ($Rows = mysql_fetch_array($Query))
{
...
}
这会将单行作为数组返回,我可以根据需要对其进行操作,例如:
if ($Rows['Reoccour'] == 1)
{
$NextDate = date('o-m-d', strtotime($Rows['Date'] . " +" . $Rows['howfar'] . " weeks"));
if ($NextDate < date('o-m-d'))
{
mysql_query("UPDATE appointments SET Date='$NextDate' WHERE ID='{$Rows['ID']}'");
}
}
这将放置在 while 循环中,以便我可以使用数组本身。但我想迁移到 PDO,但我无法获得我想要的 array 格式:
这是我的代码:
$Query = $dbh->prepare("SELECT * FROM appointments");
$Query->execute();
$Results = $Query->fetchAll();
print_r($Results);
这会产生一个多维数组,如下所示:
Array (
[0] => Array (
[ID] => 1
[0] => 1
[Appointment] => JCP Lucy
[1] => JCP Lucy
[Date] => 2012-12-21
[2] => 2012-12-21
[Location] => Grays JCP
[3] => Grays JCP
[Reoccour] => 1
[4] => 1
[howfar] => 1
[5] => 1
)
[1] => Array (
[ID] => 3
[0] => 3
[Appointment] => JCP Sign On
[1] => JCP Sign On
[Date] => 2012-12-14
[2] => 2012-12-14
[Location] =>
JCP Grays
[3] => JCP Grays
[Reoccour] => 1
[4] => 1
[howfar] => 2
[5] => 2 )
)
我想要做的是将$Results = $Query->fetchAll(); 放在像我在使用MySQL 函数时使用的while loop 这样的设置中。
如果我想将数组简化为单个字符串,我必须执行以下操作:
foreach ($Results AS $results)
{
foreach ($results AS $result)
{
echo "{$result} <br>";
}
}
但这会返回:
1
JCP Lucy
JCP Lucy
2012-12-21
2012-12-21
Grays JCP
Grays JCP
1
1
1
1
3
3
JCP Sign On
JCP Sign On
2012-12-14
2012-12-14
JCP Grays
JCP Grays
1
1
2
2
其中有重复值。我的问题是:
1) 删除重复值
2) 在使用我的 PDO Api 时执行 while loop -- 但让我能够像我在完成 mysql_fetch_array 函数时一样使用数组键。
【问题讨论】: