gpt4 book ai didi

php - 输出是数组,如何解决这个问题?

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:46 25 4
gpt4 key购买 nike

现在我正在从 Mysql 中选择一些我想输出的数据,但我不知道如何..

代码

<?php

// Database connection
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/config.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/opendb.php");

// News 1
$searchroutenews1 = "SELECT newsid FROM NewsHomepage WHERE id = '1'";
$handlenews1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews1);
$news1 = mysqli_fetch_row($handlenews1);
$searchroutenewsimg1 = "SELECT newsimg FROM NewsHomepage WHERE id = '1'";
$handlenewsimg1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg1);
$NewsImg1 = mysqli_fetch_row($handlenewsimg1);

// News 2
$searchroutenews2 = "SELECT newsid FROM NewsHomepage WHERE id = '2'";
$handlenews2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews2);
$news2 = mysqli_fetch_row($handlenews2);
$searchroutenewsimg2 = "SELECT newsimg FROM NewsHomepage WHERE id = '2'";
$handlenewsimg2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg2);
$NewsImg2 = mysqli_fetch_row($handlenewsimg2);

// News 3
$searchroutenews3 = "SELECT newsid FROM NewsHomepage WHERE id = '3'";
$handlenews3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews3);
$news3 = mysqli_fetch_row($handlenews3);
$searchroutenewsimg3 = "SELECT newsimg FROM NewsHomepage WHERE id = '3'";
$handlenewsimg3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg3);
$NewsImg3 = mysqli_fetch_row($handlenewsimg3);

?>

在此之后,我在另一个文件中 require_once 这个,然后我回显变量 $news1、$news2、$news3、$NewsImg1、$NewsImg2 和 $NewsImg3。但是,如果我现在回显这个变量,它会说:数组。

最佳答案

您可以通过单个查询获取所有这些信息,而不是您当前正在运行的 6 个。然后将 mysqli_fetch_*() 作为一段时间的参数,因为您将获取所有行,直到该函数返回 null - 此时您已经获取了所有行由查询返回。

$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT newsid, newsimg FROM NewsHomepage WHERE id IN (1, 2, 3)");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['newsid']." ".$row['newsimg'];
}

根据需要更改它以显示在 while 循环中,并按原样使用这两个变量。

或者,您可以使用 WHERE id BETWEEN 1 AND 3 代替,但是使用 IN (1, 2, 3) 可以更轻松地更改为您的确切 ID需要。

关于php - 输出是数组,如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601839/

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