函数不会保留在包装器中

时间:2014-12-09 06:43:41

标签: php html mysql css

我有一个运行查询并从MySql数据库中提取数据的函数。但是,当我运行查询时,它不会保留在div范围内,但如果我执行$content2 = 'test'之类的操作,它就会起作用。以下是我的PhP代码和CSS

function runquery()
{
$connect = mysqli_connect('localhost', 'root', 'password', 'data');
$per_page = 6;
$query = mysqli_query($connect, 'SELECT cid, fname, lname,address, score  FROM customers');
while ($query_row = mysqli_fetch_assoc($query)) {
    echo $query_row['fname'] . '<br />';
}
$content2 = runquery();
}
include 'templates.php';

这是Templates.php

<div id="content_area">
     <?php echo $content; ?>
     </div>
 <div id="content_area2">
     <?php echo $content2; ?>
 </div>

CSS:

#content_area
{
float: left;
width: 750px;
margin: 20px 0 20px 0;
padding: 10px;
}
#content_area2
{
float: left;
width: 750px;
margin: 20px 0 20px 0;
padding: 10px;
}

1 个答案:

答案 0 :(得分:1)

您的return循环中的echo而不是while()的功能需要:

function runquery() {
        $connect = mysqli_connect('localhost', 'root', 'password', 'data');
        $per_page = 6;
        $query = mysqli_query($connect, 'SELECT cid, fname, lname,address, score  FROM customers');
        while ($query_row = mysqli_fetch_assoc($query)) {
               $array[] = $query_row['fname'] . '<br />';
            }

        // Return
        return $array;
    }

// You assign the content outside the function.
$content2 = implode("",runquery());