echo数据库数据到html php pdo

时间:2014-08-27 06:34:43

标签: php html pdo

我在select语句中选择了加载html的数据库我选择了降序中的最后五个或者这里是代码

loadevent.php

<?php 
function LoadEvent(){
    global $dbh;
    $stmt = $dbh->prepare("SELECT * FROM events ORDER by event_id DESC LIMIT 5") ;
    //$stmt->bindValue(1,$eventwhat);
    $stmt->execute();
    while($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $_SESSION['searchresultwhat'] = $selected_row['event_what'];
        $_SESSION['searchresultwhere'] = $selected_row['event_where'];
        $_SESSION['searchresultwhen'] = $selected_row['event_when'];
        $_SESSION['searchresultwho'] = $selected_row['event_who'];
        $event = array($_SESSION['searchresultwhat'] => $selected_row['event_what'], $_SESSION['searchresultwhere'] =>$selected_row['event_where'], $_SESSION['searchresultwhen'] =>$selected_row['event_when'],$_SESSION['searchresultwho'] =>$selected_row['event_who']);
        $_SESSION['arr'] = $event;

        foreach ($_SESSION['arr'] as $_SESSION['searchresultwhat'] => $selected_row['event_what']){
            echo $_SESSION['searchresultwhat'];
            echo "<br/>\n";
        }
    }

}       
?>

在我的html首页我写

的index.php

<?php include_once("include/loadevent.php");
    LoadEvent();?>

在html我的代码是

<?php
    foreach ($_SESSION['arr'] as $_SESSION['searchresultwhat'] => $selected_row['event_what']){
        echo $_SESSION['searchresultwhat'];
        echo "<br/>\n";
    }
?>

当我从loadevent.php回复时,我按降序排列最后五个,但在索引中,我得到的是第一个事件,没有更多为什么这样可以让任何人看到我的错误在哪里?

3 个答案:

答案 0 :(得分:0)

简单的解决方案是将$_SESSION['arr'] = $event;替换为$_SESSION['arr'][] = $event; AND

foreach ($_SESSION['arr'] as $_SESSION['searchresultwhat'] => $selected_row['event_what']){
    echo $_SESSION['searchresultwhat'];
    echo "<br/>\n";
}

foreach ($_SESSION['arr'] as $value){
    echo $value[$_SESSION['searchresultwhat']];
    echo "<br/>\n";
}

答案 1 :(得分:0)

你正在用$ _SESSION var做一些非常奇怪的事情,据我所知,根本没有理由。试试这个:

<?php

    function LoadEvent()
    {
        global $dbh;

        $stmt = $dbh->prepare("SELECT * FROM events ORDER by event_id DESC LIMIT 5") ;
        $stmt->execute();

        $events = array();

        while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            $events[] = array(
                'searchresultwhat'  => $selected_row['event_what'],
                'searchresultwhere' => $selected_row['event_where'],
                'searchresultwhen'  => $selected_row['event_when'],
                'searchresultwho'   => $selected_row['event_who']
            );
        }

        // Is this just for testing? Or should it be echoing stuff all the time?
        // foreach ($events as $key => $value)
        // {
        //     echo $value['searchresultwhat'];
        //     echo "<br/>\n";
        // }

        // This is completely optional and should be removed if you don't
        // use the $_SESSION method later on in your HTML 
        $_SESSION['arr'] = $events;

        return $events;
    }
?>

...然后在您的HTML中,执行以下操作:

<?php

    foreach ($_SESSION['arr'] as $value)
    {
        echo $value['searchresultwhat'];
        echo "<br/>\n";
    }

?>

或者,index.php中的甚至更好执行此操作:

<?php

    include_once("include/loadevent.php");

?>

然后在您的HTML中,您可以一起避免$ _SESSION:

<?php

    foreach (LoadEvent() as $value)
    {
        echo $value['searchresultwhat'];
        echo "<br/>\n";
    }

?>

答案 2 :(得分:-1)

在'while'循环中

覆盖$_SESSION superglobal:

中的值
while($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)){
    //it is overwritten each time you are in while loop
    $_SESSION['searchresultwhat'] = $selected_row['event_what'];
    (...)

    foreach ($_SESSION['arr'] as $_SESSION['searchresultwhat'] => $selected_row['event_what']){
        //in this place when you echo it, it echoes current loop iteration so it is executed 5 times
        echo $_SESSION['searchresultwhat'];
        echo "<br/>\n";
    }
}

在index.php处,你只能从while循环回显最后一个值。