通过foreach循环PHP传递需要从数据库显示表的函数

通过foreach循环PHP传递需要从数据库显示表的函数,php,database,function,foreach,Php,Database,Function,Foreach,我正在为我网站上的管理员用户做一个概述 我遇到的问题是,我不知道如何传递像我希望传递的那样的函数。 我尝试过使用参数四处搜索,但似乎没有什么能解决我的问题 守则: 我有3个相互连接的脚本+样式表 adminUsers.php <?php session_start(); require_once ("../model/handler.php"); require_once ("../model/gui_mod.php"); echo("<link rel='stylesheet' t

我正在为我网站上的管理员用户做一个概述

我遇到的问题是,我不知道如何传递像我希望传递的那样的函数。 我尝试过使用参数四处搜索,但似乎没有什么能解决我的问题

守则: 我有3个相互连接的脚本+样式表

adminUsers.php

<?php
session_start();
require_once ("../model/handler.php");
require_once ("../model/gui_mod.php");
echo("<link rel='stylesheet' type='text/css' href='../stylesheet/stylesheet.css'>");

$gui_obj = new class_gui();

$gui_obj->admin();
$gui_obj->adminUsers();
php这个页面我使用var$conn连接到数据库

function admins(){
    ?>
    <table>
        <?php 
            $query = "SELECT * FROM admin";
            $stmt = $conn->prepare($query);
            $stmt->execute();
            $result = $stmt->fetchall();

            foreach($result as $row){
        ?>

        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>

        <?php 
            }
        ?>
    </table>
    <?php
}
gui_mod.php-这是我制作页面gui的地方

function adminUsers(){
        ?>
            <html>
                <body>

                    <div class="admin_container_head_bg">
                        <div class="admin_container_head_text_one">
                            <header>Administrator</header>
                        </div>
                        <div class="admin_container_head_text_two">
                            <p>Manager</p>
                        </div>
                    </div>
                    <div class="admin_users">
                        <div class="table_admins">

                            <?php admins(); ?>
                        </div>
                    </div>
                </body>
            </html>
        <?php
    }

我觉得这个问题是因为我在另一个函数中请求一个函数引起的。但是我真的没有任何线索,所以如果这是错误的,请告诉我:

您必须要么将连接对象声明为全局对象,要么将其传递到函数中

function admins() {
    global $conn;
    ...
或:

然后通过传入连接调用,如:

admins($conn);
否则它不在范围之内。请记住,您必须从另一个函数声明连接对象为全局对象,或者将其传递给管理员,然后从中调用管理员,否则您将遇到完全相同的问题

admins($conn);