现在我正在从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。但如果我现在回应这些变量,它会说:array。
答案 0 :(得分:2)
您可以通过单个查询获取所有这些信息,而不是当前正在运行的6个查询。然后将mysqli_fetch_*()
作为一段时间的参数放置,因为您将获取所有行,直到该函数返回null - 此时您已获取查询返回的所有行。 / p>
$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。
答案 1 :(得分:1)
首先你应该阅读http://php.net/manual/en/mysqli-result.fetch-row.php 你可以在那里找到 mysqli_result :: fetch_row - mysqli_fetch_row - 获取结果行作为枚举数组 mysqli_fetch_row总是返回数组,所以现在你不能回显数组,这就是为什么它给你数组。
您可以尝试 foreach循环或循环或循环来显示您的数据。还有各种方法来获取数组值。
以下是您可以使用的示例。
while ($news1 = mysqli_fetch_row($handlenews1)) {
echo $news1[0];
}