drsvw88664 2014-02-26 16:14
浏览 43
已采纳

在php函数中显示来自mysql的多行

I have this PHP function i am using to retrieve rows from a mysql database:

$stmt = $pdo_conn->prepare("SELECT * from admin where sequence > :sequence ");
        $stmt->execute(array(':sequence' => $user_sequence));
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $results=array();
        foreach($records as $results) {
            return $results;
        }

here i am calling the function:

$test = AdminUserSessionID2('2');
echo $test["forename"];

but it is only displaying one row, what have i done wrong which is making it not display all rows?

  • 写回答

6条回答 默认 最新

  • dongquanlin1885 2014-02-26 16:19
    关注

    Why return in foreach? Of course it will return just the first row. It's like saying foreach(rows as row){ return row; }.

    <?php
    function MyFunction($user_sequence){
        global $pdo_conn;
        $stmt = $pdo_conn->prepare("SELECT * from admin where sequence > :sequence;");
        $stmt->execute(array(':sequence' => $user_sequence));
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $records;
    }
    var_dump(MyFunction($user_sequence));
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部