简体   繁体   English

PDO取而代之

[英]PDO while fetch doing nothing

I'm trying to check first if the project exists, then if it does - I use a while loop to get the project information and output it on a smarty template. 我试图先检查项目是否存在,然后再检查-我使用while循环来获取项目信息并将其输出到一个聪明的模板上。

This code redirects me to "projects.php" if the ID doesn't exists, but if it does exists then it just simply does nothing. 这段代码将我重定向到“ projects.php”(如果该ID不存在),但是如果该ID存在,那么它什么也不做。 What am I doing wrong? 我究竟做错了什么? I would appreciate if you can guide me in the correct path. 如果您能引导我走正确的道路,我将不胜感激。

Thanks! 谢谢!

if(isset($_GET['id'])) {

try {

    $stmt = $db->prepare('SELECT * FROM projects WHERE slug = :slug');
    $stmt->execute(array(':slug' => $_GET['id']));
    $id = $stmt->fetchColumn();

    if ($id) {
        while($row = $stmt->fetch()) {
            $projects[] = $row; 
            $smarty->assign('projects',$projects); 
        }
    } else {
        header("Location: /projects.php");
        exit();
    }

} catch(PDOException $e) {
    die ('ERROR: ' . $e->getMessage());
}

}

You are passing variable to Smarty inside your while loop, it should be after while loop like: 您正在while循环内将变量传递给Smarty,它应该在while循环之后,例如:

$projects = array();
while($row = $stmt->fetch()) {
    $projects[] = $row;
}
$smarty->assign('projects',$projects); 

You are writing too much code. 您编写了太多代码。 PHP just chokes with amount and can't process further PHP只是量扼流圈和无法进一步处理

if(isset($_GET['id'])) {

    $stmt = $db->prepare('SELECT * FROM projects WHERE slug = ?');
    $stmt->execute(array($_GET['id']));
    $projects = $stmt->fetchAll();

    if ($projects) {
        $smarty->assign('projects',$projects); 
    } else {
        header("Location: /projects.php");
        exit();
    }
}

is all the code you need 是你需要的所有代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM