I'm trying to create a JSON callback. I got two files, json.html and json.php. Also, I've a database with like this:
Type: MySQL
DB Name: user_table
Table name: customers
Fields: id, name, product, supplier
Codes of my json.html is:
<html>
<head>
</head>
<body>
<div id="demo" style="font-size: 20px;"></div>
<script>
obj = { "table":"customers", "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] + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
};
xmlhttp.open("POST", "json.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
</script>
</body>
</html>
And here is the codes of json.php:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";
header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_POST["x"], false);
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>
Here is the error log reports:
PHP Notice: Undefined variable: table in /home/user/public_html/json/json.php on line 7
PHP Fatal error: Cannot access empty property in /home/user/public_html/json/json.php on line 7
PHP Notice: Undefined index: x in /home/user/public_html/json/json.php on line 4 PHP Notice: Undefined variable: table in /home/user/public_html/json/json.php on line 7
PHP Notice: Trying to get property of non-object in /home/user/public_html/json/json.php on line 7
PHP Notice: Undefined variable: limit in /home/user/public_html/json/json.php on line 7
PHP Notice: Trying to get property of non-object in /home/user/public_html/json/json.php on line 7
PHP Fatal error: Call to a member function fetch_all() on a non-object in /home/user/public_html/json/json.php on line 9
How can I make it work?
Try replacing
$result = $conn->query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit);
with
$result = $conn->query("SELECT name FROM ".$obj->{'table'}." LIMIT ".$obj->{'limit'});
Try like this...
In your javascript send object..
xmlhttp.send(obj);
In PHP:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";
header("Content-Type: application/json; charset=UTF-8");
extract($_POST); //Extracting
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT name FROM ".$table." LIMIT ".$limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
print_r($outp);
echo json_encode($outp);
?>
Looks like the JSON you are trying to send is not making it to the PHP script.
Some things I would do are:
As mentioned in a comment to David, the problem was with fetch_all(). I guess what was making the problem is the server resources because the page returned 500 on call.
In any case I retrieved the required array using this method instead:
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT * FROM customers LIMIT 10");
$outp = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$outp[] = $result->fetch_array(MYSQLI_ASSOC);
}
echo json_encode($outp);
And it worked.
Now, I'm going to make it work with my JSON callback.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.