【问题标题】:JSON save in Database and load with JQueryJSON 保存在数据库中并使用 JQuery 加载
【发布时间】:2010-11-01 18:47:50
【问题描述】:

我创建了一个巨大的 JSON 对象并将其保存在我的数据库中。但是当我加载“字符串”并在 PHP 中回显它时,我无法访问 JQuery 中的 JSON 对象。如果我想将我的 JSON 对象保存在 MySQL 数据库中,我是否需要考虑一些事情(当我只是创建数组然后用“echo json_encode($arr);”回显它时,它工作正常,但我需要保存对象用于缓存)。

{"247":{"0":"这是一个 问题","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["答案 3","961","0"],["答案 4","963","0"]]},{"248":{"0":"这是一个 问题","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["答案 3","961","0"],["答案 4","963","0"]]}}

只是摘录

如果我只是回显这个 JSON 对象,一切正常,但如果我从数据库中加载相同的字符串并回显它,它就不起作用了。

更新 1:忘记告诉我正在使用带有 UTF8_general_ci 排序规则的 TEXT-Field

更新 2:可能需要更多代码:

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

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

        return false;
    });
}

这会加载脚本并应提醒“这是一个问题”

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

?>

这是脚本,我使用 JSON-Object 加载数据库条目。

更新 3: 我想我解决了这个问题。一些中断潜入我的 JSON 对象,所以我在输出之前这样做:

$output = str_replace("\n", "", $output);
$output = str_replace("\r", "", $output);
$output = str_replace("\r\n", "", $output);

【问题讨论】:

  • 你可以使用 firebug 来检查来自服务器的响应是否真的是你所期望的吗?
  • 啊,很好的提示(我真的必须更频繁地使用 Firebug),我想我现在已经找出问题所在了。一些中断潜入我的对象,如果我这样做,在回显之前,它工作正常: $output = str_replace("\n", "", $output); $output = str_replace("\r", "", $output); $output = str_replace("\r\n", "", $output);回声$输出;
  • 提示:$output = str_replace(array("\n","\r","\r\n"), "", $output)

标签: php jquery mysql database json


【解决方案1】:

我建议查看您的 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;
    });
}

然后根据 post 参数更改 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”下的问题中进行了描述。但是如果将来我对这些东西有问题,我会使用你的方式。
【解决方案2】:

我让它以稍微不同的方式工作。我试图说明这是如何完成的。

简单的英语:

使用 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);
}

希望对你有帮助

【讨论】:

    【解决方案3】:

    也许您使用 varchar 字段,而您的字符串不适合 255 个字符?

    【讨论】:

    • 不抱歉,忘记告诉我我正在使用带有 UTF8_general_ci 排序规则的文本字段
    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多