我正在尝试从我的mysql数据库中检索记录,其中包括blob,varchar和int类型。
当我将其作为列表返回并遍历它时,它返回一个带有“Array”的连接字符串,例如“ArrayArrayArrayArrayArrayArray”
我试图用json_encode将结果包装到json中,而只是给了我一个空字符串
<?php
header("Content-Type: application/json; charset=UTF-8");
require './connection.php';
$obj = json_decode($_GET["x"], false);
$imageQuery = $db->prepare("SELECT * FROM " . $obj->page . " WHERE Type = '" . $obj->tab . "'");
$imageQuery->execute();
$resultSet = $imageQuery->get_result();
$jsonData = array();
if (mysqli_num_rows($resultSet) > 0) {
while ($array = mysqli_fetch_assoc($resultSet)) {
$jsonData[] = $array;
}
$json = json_encode($jsonData);
echo $json;
}
?>
答案 0 :(得分:0)
我对获得结果的逻辑有疑问。首先使用数据库作为抽象($ db-&gt; prepare,$ imageQuery-&gt; execute())然后尝试使用对象的连接处理程序获取行数:mysqli_num_rows($ resultSet)。
<?php
header("Content-Type: application/json; charset=UTF-8");
require './connection.php'; // create autoload.php better
//in data
$obj = json_decode($_GET["x"], false);
//sql prepare
$dhs = $db->prepare("
SELECT *
FROM " . $obj->page . "
WHERE Type = '" . $obj->tab . "'");
$dhs->execute();
$sqlResult = $dhs->get_result();
// fetch all rows
$jsonResult = array();
while ($row = $sqlResult->fetch_assoc()) {
$jsonResult[] = $row;
}
//return json array
echo json_encode($jsonResult);