Json文件创建

时间:2017-06-29 11:00:16

标签: javascript php json

我正在寻找如何从mysql数据库创建json文件的解决方案。这是一个代码:

*

<!DOCTYPE html>
<html>
<body>
<p id="container"></p>
<script>
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "table":"produkty", "limit":10 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        for (x in myObj) {
            txt += myObj[x].name + "<br>";
        }
        document.getElementById("demo").innerHTML = txt;
    }   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
};
xmlhttp.open("POST", "test4.php", true);
xmlhttp.setRequestHeader("Content-type", "text/xml");
xmlhttp.send("x=" + dbParam);
</script>
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj =  json_decode($_POST["x"], false);

$conn = new mysqli("localhost", "root", "", "projekt_1");
$result = $conn->query("SELECT produkt FROM ".$obj->table." LIMIT ".$obj->limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>
</body>
</html>

*

在JSON文件中我收到了信息:

<br />
<b>Notice</b>:  Undefined index: x in <b>C:\xampp\htdocs\projekt_1\test4.php</b> on line <b>31</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\projekt_1\test4.php</b> on line <b>34</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\projekt_1\test4.php</b> on line <b>34</b><br />
<br />
<b>Fatal error</b>:  Call to a member function fetch_all() on boolean in <b>C:\xampp\htdocs\projekt_1\test4.php</b> on line <b>36</b><br />

不确定错误在哪里。我是新手,所以我的问题很明显。

提前致谢。

我是这个论坛中的新人,我应该先检查我的问题的答案,但是当我将下面的建议包含在代码中并在apache白色屏幕上运行时。我从数据库中得到了前10行。我已经检查了白屏出现时可以执行的步骤。整个代码包含在一个文件test4.php中:

    <?php
ini_set('display_errors', 1); error_reporting(~0);
?>

<!DOCTYPE html>

<html lang="pl">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>Test file</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>

<body>

<p id="container"></p>

<script>
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "table":"produkty", "limit":10 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        for (x in myObj) {
            txt += myObj[x].name + "<br>";
        }
        document.getElementById("container").innerHTML = txt;
    }   
};
xmlhttp.open("POST", "test4.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

</script>

<?php

if (count($_POST)) {
    header("Content-Type: application/json; charset=UTF-8");
    $obj =  json_decode($_POST["x"], false);

    $conn = new mysqli("localhost", "root", "", "projekt_1");
    $result = $conn->query("SELECT produkt FROM ".$obj->table." LIMIT ".$obj->limit);
    $outp = array();
    $outp = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode($outp);
}
?>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

甚至在您开始POST请求之前,您就是在尝试访问$_POST数组。所以你需要检查一下。

<?php
if (count($_POST)) {
    header("Content-Type: application/json; charset=UTF-8");
    $obj =  json_decode($_POST["x"], false);

    $conn = new mysqli("localhost", "root", "", "projekt_1");
    $result = $conn->query("SELECT produkt FROM ".$obj->table." LIMIT ".$obj->limit);
    $outp = array();
    $outp = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode($outp);
}
?>
</body>

通过这种方式,header和其他内容只有在全局$_POST数组中找到任何内容时才会执行。这也意味着通过POST方法请求页面。