在此脚本中使用PDO

时间:2013-04-17 17:00:52

标签: php mysql pdo

我正在尝试使用PDO重写此脚本:

从数据库中获取值

mysql_select_db($database_db_FPF, $db_FPF);
$query_rsWorksMenu = "SELECT works.year, GROUP_CONCAT(workstitle.title) as titulos, GROUP_CONCAT(workstitle.id_title) as links FROM works JOIN workstitle ON works.id_year =      workstitle.id_year GROUP BY works.year ORDER BY works.year DESC";
$rsWorksMenu = mysql_query($query_rsWorksMenu, $db_FPF) or die(mysql_error());
$row_rsWorksMenu = mysql_fetch_assoc($rsWorksMenu);

显示值

     <ul>
        <?php do { ?>
        <li><span><?php echo $row_rsWorksMenu['year']; ?></span>
            <ul>
                <?php 
                 $titulos = explode(",", $row_rsWorksMenu['titulos']);
                 $links = explode(",", $row_rsWorksMenu['links']); 
                 foreach(array_combine($links, $titulos) as $link => $titulo){ 
                ?>
                <li><span><a href="works.php?id=<?php echo $link; ?>"><?php echo $titulo; ?></a></span></li>
                <?php } ?>
            </ul>
        </li>
        <?php }while ($row_rsWorksMenu = mysql_fetch_assoc($rsWorksMenu)); ?>
    </ul>

到目前为止我得到了什么:

从数据库中获取值:

$stmt2 = $conn->prepare('SELECT works.year, GROUP_CONCAT(workstitle.title) as titulos, GROUP_CONCAT(workstitle.id_title) as links FROM works JOIN workstitle ON works.id_year = workstitle.id_year GROUP BY works.year ORDER BY works.year DESC');
$stmt2->execute();
$result2 = $stmt2->setFetchMode(PDO::FETCH_OBJ);

显示值

        <ul>
        <?php do { ?>
        <li><span><?php echo $result2->year; ?></span>
            <ul>
                <?php 
                 $titulos = explode(",", $result2->titulos);
                 $links = explode(",", $result2->links); 
                 foreach(array_combine($links, $titulos) as $link => $titulo){ 
                ?>
                <li><span><a href="works.php?id=<?php echo $link; ?>"><?php echo $titulo; ?></a></span></li>
                <?php } ?>
            </ul>
        </li>
        <?php }while ($row_rsWorksMenu = mysql_fetch_assoc($rsWorksMenu)); ?>
    </ul>

问题是我不知道如何处理这一行:

}while ($row_rsWorksMenu = mysql_fetch_assoc($rsWorksMenu));

更新

我改变了我的代码:

$stmt2 = $conn->prepare('...query...');
$stmt2->execute(); 
$result2 = $stmt2->fetchAll();

}while ($row_rsWorksMenu = $stmt2->fetchAll());

但没有任何反应。

更新2

我试试这段代码:

        <?php do { ?>
        <li><span><?php echo $result2->year; ?></span>
            <ul>
                <?php 
                 $titulos = explode(",", $result2->titulos);
                 $links = explode(",", $result2->links); 
                 foreach(array_combine($links, $titulos) as $link => $titulo){ 
                ?>
                <li><span><a href="works.php?id=<?php echo $link; ?>"><?php echo $titulo; ?></a></span></li>
                <?php } ?>
            </ul>
        </li>
        <?php }while($result2 = $stmt2->fetch()); ?>

我的DOM显示<li>的正确数字。但没有价值观。为什么呢?

2 个答案:

答案 0 :(得分:1)

最简单的方法是:

$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($conn->query($sql) as $row) {
    print $row['name'] . "\t";
    print $row['color'] . "\t";
    print $row['calories'] . "\n";
}

PDO query方法返回PDOStatement,您可以直接迭代。

http://www.php.net/manual/de/pdo.query.php

答案 1 :(得分:1)

完全放弃while循环。您也不需要单独准备语句,因为您的查询不使用任何查询参数。

请改为:

// Get all your DB results in one place
$rsWorksMenu = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);

稍后,要显示结果:

<?php if ($rsWorksMenu):?>
<ul>
<?php     foreach($rsWorksMenu as $r):
             $links_titulos = array_combine(
                explode(",", $r->titulos),
                explode(",", $r->links)
             );
?>
    <li><span><?=h($r->year)?></span>
<?php        if ($links_titulos):?>
        <ul>
<?php            foreach($links_titulos as $link => $titulo):?>
            <li><span><a href="works.php?id=<?=h(urlencode($link))?>"><?=h($titulo)?>a></span></li>
<?php            endforeach // $links_titulos?>
        </ul>
<?php        endif //$links_titulos?>
    </li>
<?php     endforeach // $rsWorksMenu?>
</ul>
<?php endif // $rsWorksMenu?>

您也应该更改您的sql语句 - 如果您的任何链接或标题中包含逗号,它将会中断。 (GROUP_CONCAT通常是一个坏主意。)

相反,收集未分组的值并自己将它们分组到php数组中,如下面的get_worksmenu函数所示:

<?php

function get_worksmenu(PDO $db) {
    $rsWorksMenu = array();
    $sql = 'SELECT works.year, workstitle.title AS titulos, workstitle.id_title AS links '
           .'FROM works INNER JOIN workstitle USING (id_year) ORDER BY year DESC';
    // Could this be a single-table query instead?
    //   SELECT id_year AS year, title AS titulos, id_title AS links
    //   FROM workstitle ORDER BY id_year DESC
    $rs = $db->query($sql);
    $rs->setFetchMode(PDO::FETCH_ASSOC);
    foreach ($rs as $r) {
        // calculating url here simplifies your display code
        // and makes it easy to change urls later
        $year = $r['year'];
        $r['url'] = '/works.php?id='.urlencode($r['links']);
        unset($r['year']);
        $result[$year][] = $r;
    }
    $rs->closeCursor();
    return $rsWorksMenu;
}
function h($s) {
    // escape a string for html
    return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
}


$rsWorksMenu = get_worksmenu($conn);

<?php if ($rsWorksMenu):?>
<ul>
<?php   foreach($rsWorksMenu as $year => $works):?>
  <li><span><?=h($year)?></span>
    <ul>
<?php       foreach($works as $work):?>
        <li><span><a href="<?=h($work['url'])?>"><?=h($work['titulos'])?></a></span></li>
<?php       endforeach //$work?>
    </ul>
<?php   
  </li>
<?php   endforeach // $rsWorksMenu?>
</ul>
<?php endif // $rsWorksMenu?>