Storing JSON in a database and loading using jQuery

I create a huge JSON object and save it in my database. But when I load the “string” and echo into PHP, I cannot access the JSON object in jQuery. Do I have to consider something if I want to save my JSON object in a MySQL database (when I just create an array and then echo it using "echo json_encode ($ arr)", it works fine, but I need to save the object for caching).

{"247": {"0": "This is Question" "1": "," 2 ":" 247 "," 3 ":" 0 "," answers ": [[" Answer1 "," 960 ", "1"], ["Answer 2", "962", "0"], ["Answer 3", "961", "0"], ["Answer 4", "963", "0"]] }, {"248": {"0": "This is Question" "1": "," 2 ":" 247 "," 3 ":" 0 "," answers ": [[" Answer1 "," 960 "," 1 "], [" Answer 2 "," 962 "," 0 "], [" Answer 3 "," 961 "," 0 "], [" Answer 4 "," 963 "," 0 " ]]}}

just an excerpt

If I just repeat this JSON-Object, everything works fine, but if I load the same row from the database and the echo file, this will not work.

Update 1: forget to tell me that I am using the TEXT field with the setting UTF8_general_ci

Update 2: Maybe a little more code:

function start() {
    $(".start").click(function () {

        $.post("load_script.php", { }, function(data){
            alert(data[247][0]);
        }, "json");

        return false;
    });
}

loads the script and should warn "This is a question"

<?php
require_once('connect.php');

$ergebnis = mysql_query("SELECT text FROM cache_table ORDER BY RAND() LIMIT 1");
while($row = mysql_fetch_object($ergebnis)) {
    $output = $row->text;
}

echo $output;

? >

this is a script where I load a database record using JSON-Object.

Update 3: I think I solved the problem. Some violation got into my JSON-Object, so I do this before exiting:

$output = str_replace("\n", "", $output);
$output = str_replace("\r", "", $output);
$output = str_replace("\r\n", "", $output);
+3
source share
3 answers

, javascript. , jQuery json , :

function start() {
    $(".start").click(function () {

        $.post("load_script.php", { }, function(data){
                alert(data);
        }, "text");

        return false;
    });
}

, - UTF-8, .

, , :

var data1, data2;
function start() {
    $(".start").click(function () {

        $.post("load_script.php", {src: "db" }, function(data){
                data1 = data;
        }, "text");

        $.post("load_script.php", {src: "echo" }, function(data){
                data2 = data;
        }, "text");

        if (data1 == data2) {
           alert("data1 == data2");
        }
        else {
           var len = data1.length < data2.length ? data1.length : data2.length;
           for(i=0; i<len; ++i) {
              if (data1.charAt(i) != data2.charAt(i)) {
                 alert("data1 first differs from data2 at character index " + i);
                 break;
              }
           }
        }

        return false;
    });
}

PHP-, , , :

<?php
   if ($_POST['src'] == 'db')) {
      require_once('connect.php');

      $ergebnis = mysql_query("SELECT text FROM cache_table ORDER BY RAND() LIMIT 1");
      while($row = mysql_fetch_object($ergebnis)) {
        $output = $row->text;
      }
   }
   else {
      $output = '{"247":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]},{"248":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]}}';
   }

echo $output;
?>

, !

+3

. , .

:

urldecode()

$json    = $this->getContent($url);  // CURL function to get JSON from service
$result  = json_decode($json, true); // $result is now an associative array

...

$insert  = "INSERT INTO mytable (url, data) ";
$insert .= "VALUES('" . $url . "', '" . urlencode(json_encode($result)) . "') ";
$insert .= "ON DUPLICATE KEY UPDATE url=url";

...

/*
** Figure out when you want to check cache, and then it goes something like this
*/

$sqlSelect = "SELECT * FROM mytable WHERE url='" . $url . "' LIMIT 0,1";

$result = mysql_query($sqlSelect) or die(mysql_error());
$num    = mysql_numrows($result);

if ($num>0) {
    $row   = mysql_fetch_assoc($result);
    $cache = json_decode(urldecode($row['data']), true);
}

,

+2

, varchar, 255 ?

+1

Source: https://habr.com/ru/post/1710147/


All Articles