我正在尝试学习如何将查询转换为PDO。我终于得到了一个简单的行(计数)查询,现在我正在处理更复杂的查询。
我目前正在使用以下查询。 (对不起,它太复杂了;我还在努力简化它。)
$SeaURL = str_replace('Washington/', '', $MyURL);
$res = mysql_query ("SELECT P.URL, P.Title, P.Subtitle, P.MetaTitle, P.MetaDesc, P.KW, P.Live, PB.Common, PB.First, PB.Middle, PB.Last, PB.Prefix, PB.Suffix, PB.Born, PB.Died, PB.Birth_Place, PB.Death_Place, PB.Nationality, Brf.Site, Brf.Brief, PC.Class, PX.Article PXA, PX.Pagedex PXP, IVR.Article IVRA, IVR.Pagedex IVRP, SM.Article SMA, SM.Pagedex SMP
FROM people P
LEFT JOIN people_1_bio PB ON PB.URL = P.URL
LEFT JOIN people_1_class PC ON PC.URL = P.URL
LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
LEFT JOIN people_articles_px PX ON PX.URL = P.URL
LEFT JOIN people_articles_ivr IVR ON IVR.URL = P.URL
LEFT JOIN people_articles_sm SM ON SM.URL = P.URL
WHERE P.URL LIKE '$MyURL' AND P.Site = '$MySiteID'
GROUP BY Class") or die (mysql_error());
使用这个特定的查询,我需要一个while循环来定义一些值,其中一些我进一步放入数组(例如$ ClassList和$ ClassLinked)。
while ($row = mysql_fetch_array ($res))
{
$URL = $row['URL'];
$Title = $row['Title'];
$Class = $row['Class'];
$ClassList[] = $row['Class'];
$ClassL = str_replace(' ', '_', $Class);
$ClassLinked[] = '<a href="/People/'.$ClassL.'" title="'.$Class.'">'.$row['Class'].'</a>';
}
通过上述数据,我可以显示页面标题(例如$ Title = Carl Sagan)和他所属的类列表(数组)(例如$ ClassList [] =科学家|作家|政治活动家)。
这是我的PDO改造:
$dsn = "mysql:host=localhost;dbname=DATABASE;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'USERNAME','PASSWORD', $opt);
$SeaURL = str_replace('Washington/', '', $MyURL);
$stm = $pdo->prepare("SELECT P.URL, P.Title, P.Subtitle, P.MetaTitle, P.MetaDesc, P.KW, P.Live, PB.Common, PB.First, PB.Middle, PB.Last, PB.Prefix, PB.Suffix, PB.Born, PB.Died, PB.Birth_Place, PB.Death_Place, PB.Nationality, Brf.Site, Brf.Brief, PC.Class, PX.Article PXA, PX.Pagedex PXP, IVR.Article IVRA, IVR.Pagedex IVRP, SM.Article SMA, SM.Pagedex SMP
FROM people P
LEFT JOIN people_1_bio PB ON PB.URL = P.URL
LEFT JOIN people_1_class PC ON PC.URL = P.URL
LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
LEFT JOIN people_articles_px PX ON PX.URL = P.URL
LEFT JOIN people_articles_ivr IVR ON IVR.URL = P.URL
LEFT JOIN people_articles_sm SM ON SM.URL = P.URL
WHERE P.URL LIKE '$MyURL' AND P.Site = '$MySiteID'
GROUP BY Class");
$stm->execute();
$data = $stm->fetchAll();
我认为我的查询不起作用,但是当我不知道如何显示结果时很难检查它。这就是我在这里问的问题。
如果我正确理解了教程@ https://stackoverflow.com/tags/pdo/info,fetchAll()函数会替换while循环,对吗?
我的原始代码定义了以下值,然后我可以在单独的文件中回显:
$Class = $row['Class'];
$ClassList[] = $row['Class'];
使用我的新PDO脚本定义$ Class和$ ClassList的最佳方法是什么?
页面@ https://stackoverflow.com/tags/pdo/info使用foreach函数提供了一个非常混乱(且不完整?)的示例......
?>
<table>
<? foreach ($data as $row): ?>
<tr>
<td>
<a href="news.php?<?=$row['id']?>">
<?=htmlspecialchars($row['name'])?>
</a>
</td>
</tr>
<? endforeach ?>
但我不打算在表格中显示所有数据。我如何将$ Class定义为单个值,将$ ClassList定义为一系列值(数组)?
答案 0 :(得分:0)
使用PDOStatement fetch功能。在您的PDO改造代码中,删除行
$data = $stm->fetchAll();
使用
while( $row = $stm->fetch(PDO::FETCH_ASSOC ) {
而是在循环的开头处理值。
还有一种更好的方法可以使用prepare()
$stm = $pdo->prepare('SELECT P.URL, P.Title, P.Subtitle, P.MetaTitle, P.MetaDesc, P.KW, P.Live, PB.Common, PB.First, PB.Middle, PB.Last, PB.Prefix, PB.Suffix, PB.Born, PB.Died, PB.Birth_Place, PB.Death_Place, PB.Nationality, Brf.Site, Brf.Brief, PC.Class, PX.Article PXA, PX.Pagedex PXP, IVR.Article IVRA, IVR.Pagedex IVRP, SM.Article SMA, SM.Pagedex SMP
FROM people P
LEFT JOIN people_1_bio PB ON PB.URL = P.URL
LEFT JOIN people_1_class PC ON PC.URL = P.URL
LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
LEFT JOIN people_articles_px PX ON PX.URL = P.URL
LEFT JOIN people_articles_ivr IVR ON IVR.URL = P.URL
LEFT JOIN people_articles_sm SM ON SM.URL = P.URL
WHERE P.URL LIKE :url AND P.Site = :site
GROUP BY Class',
array( ) );
$stm->execute( array( ':url'=> $MyURL, ':site' => $MySiteID ) );
while( $row = $stm->fetch(PDO::FETCH_ASSOC ) {
// your loop here