是否需要从服务器检索数据的SQL数据库?

Sorry but I'm a newbie to web development. I've been playing around with some JSON objects lately and have been using some example from W3school site - 'http://www.w3schools.com/json/json_example.asp.'

The example retrieves data from MYSQLi db and injected it into a JSON object. I've been using this example to create a dynamic site however my problem is I'm not sure if you need a db to store the data in the first place. Can JSON object be directly retrieved from a server?

The response from the server is parsed as a JSON object. i.e. var = JSON.parse(response)

<!DOCTYPE html>

<html>

<head>

<title>XMLHTTPREQUESTEXAMPLE</title>

</head>

<body>

<p id="id01"></p>

<script>

var xmlhttp = new XMLHttpRequest();

var url = "//http:URL";

xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        myFunction(xmlhttp.responseText);

    }else{

                    document.getElementById("id01").innerHTML = "Server not responding";

                }

}

xmlhttp.open("GET", url, true);

xmlhttp.send();



function myFunction(response) {

    var arr = JSON.parse(response);

    var i;

    var out = "<table><tr><th>Name</th><th>Job Title</th><th>Room No</th><th>Tel Ext</th><th>Email</th></tr>";



    for(i = 0; i < arr.length; i++) {

        out += "<tr><td>" + 

        arr[i].Name +

        "</td><td>" +

        arr[i].JobTitle +

        "</td><td>" +

        arr[i].RoomNo +

        "</td></tr>"

                                arr[i].TelExt +

        "</td></tr>"

                                arr[i].Email +

        "</td></tr>";

    }

    out += "</table>"

    document.getElementById("id01").innerHTML = out;

}

</script>

</body>

</html>

The example php script on the server from the W3 school site.

$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");

$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
    $outp .= '"City":"'   . $rs["City"]        . '",';
    $outp .= '"Country":"'. $rs["Country"]     . '"}'; 
}
$outp .="]";

$conn->close();

echo($outp);
?>

That example uses a MySQL database to retrieve data, but you can simply echo out your own data just to test your AJAX with JSON example like so:

<?php

$array = array(
"Key" => "value",
"Foo" => "Bar"
);

$jsonData = json_encode($array);

echo ($jsonData);

?>

Then you can retrieve the values in your Javascript's myFunction by doing :

arr.Foo; //would return Bar

A database is just for storing long-term data, it is not needed for the functional part of this tutorial.

I think the most appropriate question would be, "Is a database needed for this type of data/application?" Or, "Should I use a db for this type of application?" Technically speaking, no, you don't need a database to store and retrieve information from the server, but depending on what you are trying to achieve, a database is probably going to be the wisest solution.

have you tried json_encode($value)? http://php.net/manual/en/function.json-encode.php

this encodes an array into json object, and of course - you can store json data in any type of file, not only in database