0

我已经尝试了 10 分钟,但找不到方法。我将几个名称放入数据库,并mysqli_fetch_array()在一个名为的函数中调用这些名称sort_member_db()

我在函数中的输出代码是:echo $names['FirstName']. ' ' . $names['LastName']; echo '<br>';

它正确打印所有名称,但是名称不在我希望它们所在的 div 元素中。它们显示在网站的最顶部。

这是我在 div 元素中放入的内容:

 <div class="myclass">
            <hgroup class="title">
                <h1>Text1</h1>
                <h2>
                     text2
                </h2>
            </hgroup>
            <p>
                Array should print after this line:<br>

             '. sort_member_db() .'

            </p>
        </div>

编辑:这是整个功能

function sort_member_db()
{

    $openConn = mysqli_connect(
            "host", "user", "", "");

    //The sorting function for mysql goes here
    $sortedMembers = mysqli_query($openConn, "SELECT * FROM Members ORDER BY LastName");

    //Get the names and insert them into a PHP array
    while($names = mysqli_fetch_array($sortedMembers))
    {
        //Escape sequence to find the possible error
        if (!$sortedMembers)
        {
            printf("Error: %s\n", mysqli_error($openConn));
            exit();
        }

        //Print out the names here        
        echo $names['FirstName']. ' ' . $names['LastName'];
        echo '<br>';
    }

    mysqli_close($openConn);

}

这是我试图将数组放入的 div 元素:

page_body('        <div id="body">
            <!-- RenderSection("featured", required:=false) -->
            <section class="content-wrapper main-content clear-fix">
                <!-- @RenderBody() -->
                    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>Members:</h1>
                <h2>

                </h2>
            </hgroup>
            <p>
                Our Team Members :<br>

             '. sort_member_db() .'

            </p>
        </div>
            </section>
            </section>');
4

1 回答 1

1

您需要返回并连接 HTML 中的名称,而不是直接在被调用函数中回显名称,否则将首先完成。

function sort_member_db()
{

    $openConn = mysqli_connect(
            "host", "user", "", "");

    //The sorting function for mysql goes here
    $sortedMembers = mysqli_query($openConn, "SELECT * FROM Members ORDER BY LastName");

    //Get the names and insert them into a PHP array
    $returnString = '';
    while($names = mysqli_fetch_array($sortedMembers))
    {
        //Escape sequence to find the possible error
        if (!$sortedMembers)
        {
            printf("Error: %s\n", mysqli_error($openConn));
            exit();
        }

        //Print out the names here        
        $returnString .= $names['FirstName']. ' ' . $names['LastName'];
        $returnString .='<br>';
    }

    mysqli_close($openConn);
    return $returnString;
}
于 2013-10-27T23:12:15.393 回答