0

I'm looking for solution how to create json file from mysql data base. Here is a code :

*

<!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>

*

in JSON file I recievied information:

<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 />

not sure where is mistake. I'm pretty new guys so my question can be obvious.

Thanks in advance.

I'm new one in this forum and I should have checked answer to my question before but when I included the suggestion as below to the code and and run it on a apache white screens comes. I've epected first 10 rows from data base. I've checked the steps what can be done when white screen appear. the whole code which is included in one file 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 Answers1

1

Even before you start your POST request, you are trying to access the $_POST array. So you need to have a check.

<?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>

In this way, the header and other things gets executed only when it finds any content in the global $_POST array. This also means that the page is requested via POST method.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252