错误来自$ stmt-> bind_result(); 您的MySQL查询从数据库中选择1列,并绑定5列,这是不可能的。这里是应该正常工作:
$db_host = "localhost";
$db_name = "nm";
$db_user = "root";
$db_pass = "";
$mysqli_news = new mysqli($db_host, $db_user, $db_pass, $db_name);
function topbanner()
{
global $mysqli_news;
$stmt = $mysqli_news->prepare("SELECT date FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
$stmt->execute();
$stmt->bind_result($date);
while ($stmt->fetch()){
echo $date; // echo/return $date column
}
$stmt->close();
}
echo topbanner();
或为topbanner()如果你想获得在数据库中,使用的所有数据:
function topbanner()
{
global $mysqli_news;
$stmt = $mysqli_news->prepare("SELECT * FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
$stmt->execute();
$stmt->bind_result($id, $title, $image, $link, $source, $date);
while ($stmt->fetch()){
echo $date; //echo whatever column you want
}
$stmt->close();
}
而且,知道,这取决于你如何想要使用函数,当你使用“return”的时候,你在while循环的第一次出现之后结束函数,如果你想获得所有可用的值,你可以将结果附加到while循环的变量中,在函数结尾处返回变量,如下所示:
function topbanner()
{
global $mysqli_news;
$stmt = $mysqli_news->prepare("SELECT * FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
$stmt->execute();
$stmt->bind_result($id, $title, $image, $link, $source, $date);
while ($stmt->fetch()){
$result .= $date; //Append results to a variable
}
return $result;
$stmt->close();
}
因此,与上面的代码,你可以继续呼应功能:
echo $topbanner();
我应该:'$ mysqli_news'可以和它在哪儿定义? – Rizier123
@ Rizier123我eddited它 –