用while调用php函数

用while调用php函数,php,mysql,database,Php,Mysql,Database,我是PHP新手 我想从数据库中获取POST数据,所以我创建了这个函数 function GetPosts() { global $connection; global $post_id; global $post_cat_id; global $post_authur; global $post_date; global $post_image; global $post_content; global $post_status; global $post_tags; global $post_

我是PHP新手 我想从数据库中获取POST数据,所以我创建了这个函数

function GetPosts() {
global $connection;
global $post_id;
global $post_cat_id;
global $post_authur;
global $post_date;
global $post_image;
global $post_content;
global $post_status;
global $post_tags;
global $post_title;
$query = "SELECT * FROM posts";
$get_all_posts = mysqli_query($connection, $query);
    if(!$get_all_posts) {
         die(mysqli_error($connection));
    }
while($row = mysqli_fetch_assoc($get_all_posts)) {
        $post_id = $row['post_id'];
        $post_cat_id = $row['post_category_id'];
        $post_authur = $row['post_authur'];
        $post_title = $row['post_title'];
        $post_date = $row['post_date'];
        $post_image = $row['post_image'];
        $post_content = $row['post_content'];
        $post_status = $row['post_status'];
        $post_tags = $row['post_tags'];
    } }
我调用了posts.php文件中的函数,但只显示了一篇文章

<?php GetPosts(); ?>
                                    <tr>
                                    <td><?php echo $post_id ?></td>
                                    <td><?php echo $post_authur ?></td>
                                    <td><?php echo $post_title ?></td>
                                    <td><?php echo $post_cat_id ?></td>
                                    <td><?php echo $post_status ?></td>
                                    <td><?php echo $post_image ?></td>
                                    <td><?php echo $post_tags ?></td>
                                    <td><?php echo $post_date ?></td>


我应该如何调用此函数来获取所有帖子?

如果您想要查看代码,请访问否我需要一种通过调用此函数获取所有帖子的方法从数据库中获取数据没有问题。我已将所需的函数设置为全局函数。问题是,当我试图在表中显示它们时,while并没有回显每个帖子(查看第一篇帖子中的第二个代码),变量必须在函数外部可用。尝试将其变为可在函数之外使用的变量。但我只能得到1个帖子的数据。
function GetPosts() {
    global $connection;
    $result = [];
    $query = "SELECT * FROM posts";
    $get_all_posts = mysqli_query($connection, $query);
    if(!$get_all_posts) {
        die(mysqli_error($connection));
    }
    while($row = mysqli_fetch_object($get_all_posts)) {
        $result[] = $row;
    } 
    return $result;
}

$posts = GetPosts();
foreach ($posts as $post) {
    echo $post->post_id; // here you can access all the attributes in column
}