gpt4 book ai didi

php - 功能不在包装器内

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:43 26 4
gpt4 key购买 nike

我有一个函数可以运行查询并从 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;
}

最佳答案

您的函数需要在 while() 循环中return 而不是 echo:

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());

关于php - 功能不在包装器内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com