4

我正在尝试使用 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>. 但没有价值观。为什么?

4

2 回答 2

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";
}

PDOquery方法返回一个PDOStatement您可以直接迭代的。

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

于 2013-04-17T18:32:25.013 回答
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?>
于 2013-04-17T19:18:09.560 回答