2017-06-29 106 views
0

我正在尋找解決方案如何從MySQL數據庫創建json文件。下面是一個代碼: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文件我recievied信息:

<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> 
+5

所有這些錯誤消息已經討論了很多次,所以請去_research_他們。 – CBroe

+2

這是一個文件嗎?你應該有一個單獨的腳本,用於你的ajax調用,它只輸出json而不是其他東西。 – jeroen

+0

也顯示test4.php。 – YvesLeBorg

回答

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方法請求。