如何在$ result-> fetch_assoc()中包含数组?

时间:2019-06-11 12:31:51

标签: php

我不知道如何在$ result-> fetch_assoc中包含数组。

我只想在$ result的每一行中包含countryName [1]和categoryName [2]

$sql = "SELECT pic, title, address, map_id, category FROM ap_234 WHERE 
COALESCE(pic, '') != '' ORDER BY id DESC";
$result = $conn->query($sql);

$sql = "SELECT DISTINCT substring(post_content, 13, LENGTH(post_content) 
-14), post_title FROM ap_235 WHERE post_content LIKE '[abc id%' AND post_title 
NOT IN ('a', 'b')";
$countryName = $conn->query($sql);

$sql = "SELECT category_name FROM ap_255_categories WHERE category_name 
NOT IN ('a', 'b' , 'c', 'd')";
$categoryName = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {

echo '<br> <img src="'.$row['pic'].'"><br>Name: '. $row['title']. '<br>Coordinates: ' . $row['address'] . '<br>Country: ' . $countryName[1] . '<br>Category: ' . $categoryName[0] . '<br>';
}
} else {
echo "0 results";
}    

错误消息:不能将mysqli_result类型的对象用作数组。

1 个答案:

答案 0 :(得分:0)

在您的$countryName中是SQL结果,而不是结果数组。首先准备$countryName数组。

$countryNameResult = $conn->query($sql);
$countryName = [];
while($row = $countryNameResult->fetch_assoc()) {
    $countryName[] = $row; // or direct any column from DB, $row['col']
}

$categoryName相同。