PHP-查询mySQL数据库并以JSON格式显示结果时失败

PHP-查询mySQL数据库并以JSON格式显示结果时失败,php,mysql,json,Php,Mysql,Json,因此,我们最近为我们大学的一个项目建立了一个mySQL数据库,目前正在处理用php编写的数据访问文件。为了使用前端的数据,我们需要将其格式化为JSON。当在某个对象的结果中放置一个对象数组时,它将不适用于2个对象数组。 工作代码: <?php include_once 'db.php'; $email = "email@online.com"; $conn = connect(); $conn->set_charset('utf8'); $resultArr = array()

因此,我们最近为我们大学的一个项目建立了一个mySQL数据库,目前正在处理用php编写的数据访问文件。为了使用前端的数据,我们需要将其格式化为JSON。当在某个对象的结果中放置一个对象数组时,它将不适用于2个对象数组。 工作代码:

<?php
include_once 'db.php';

$email = "email@online.com";

$conn = connect();
$conn->set_charset('utf8');

$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);

    $sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
    $result2 = $conn->query($sql2);
    while($row2 = $result2->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['shops'][] = $row2;
    }

}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);

?>
<?php
include_once 'db.php';

$email = "email@online.com";

$conn = connect();
$conn->set_charset('utf8');

$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);

    $sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
    $result2 = $conn->query($sql2);
    while($row2 = $result2->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['shops'][] = $row2;
    }

    $sql3 = "SELECT img, name FROM ad INNER JOIN rel_customer_ad WHERE customer_email='".$row['email']."' AND rel_customer_ad.ad_name = ad.name";
    $result3 = $conn->query($sql3);
    while($row3 = $result3->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['ads'][] = $row3;
    }
}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
   echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);

?>

下面是不起作用的代码:

<?php
include_once 'db.php';

$email = "email@online.com";

$conn = connect();
$conn->set_charset('utf8');

$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);

    $sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
    $result2 = $conn->query($sql2);
    while($row2 = $result2->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['shops'][] = $row2;
    }

}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);

?>
<?php
include_once 'db.php';

$email = "email@online.com";

$conn = connect();
$conn->set_charset('utf8');

$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);

    $sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
    $result2 = $conn->query($sql2);
    while($row2 = $result2->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['shops'][] = $row2;
    }

    $sql3 = "SELECT img, name FROM ad INNER JOIN rel_customer_ad WHERE customer_email='".$row['email']."' AND rel_customer_ad.ad_name = ad.name";
    $result3 = $conn->query($sql3);
    while($row3 = $result3->fetch_assoc()) {
        $resultArr['customer'][$row['email']]['ads'][] = $row3;
    }
}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
   echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);

?>


当直接在数据库上运行时,这两个SQL查询都提供了预期的结果,我不知道为什么第二个查询在PHP脚本中不起作用。

你能定义你要买的东西吗不起作用请第一个被剪掉的代码给出了结果:{“客户”:[{“电子邮件”:”bla@bla.de“,”地址“:”00001家乡\r\n家庭街123号“,”商店“:[{”id:”1“,”名称“:“WM_FLO_123”,“地址”:“pimewqöofmwe\r\nwpokmfwlkmefk”}]}正如预期的那样。但是第二个插件在没有给出错误消息的情况下不会回显任何内容。@YashParekh这将是危险的,因为OP没有使用
mysql\uuu
数据库扩展,原因很明显,例如旧的扩展已经死亡并被掩埋,因为它很危险,那么您将不得不恢复到老式的调试。添加
print\r($resultArr);
在代码中的不同位置,这样您就可以看到某些东西在哪里对您的用户做了一些有趣的事情array@Donkarnash请仔细阅读PHP中双引号文本中的变量扩展