从php回来的空jsonencoded数据

时间:2014-04-13 14:42:59

标签: php mysql json

我正在将我的网站从一台服务器移动到另一台服务器。我已导出旧的php数据库并将其导入新服务器。数据库名称和登录保持不变(暂时)。 目前数据库中有2个表。对于一个我的PHP请求工作完全正常,一个它没有,我不知道什么可能是错的。登录文件不会抛出错误,因此它们似乎工作正常。有人可以帮忙吗? 这是不起作用的代码:

    <?php
    ini_set('display_errors', 1);

//phpinfo();
    require '.ht-loginWriting.php';
    $myquery="SELECT ID, title, shortDescription, text,date, referenceProject, workshop, otherTag FROM writings";
    $query=mysql_query($myquery);
    if(! $myquery){
        echo mysql_error();
        die;
    }
    $data=array();
    for($x=0; $x < mysql_num_rows($query); $x++){
        $data[]=mysql_fetch_assoc($query);
    }
    echo json_encode($data);


?>

.HT-loginWriting.php

    <?php
    $dbhost = 'localhost:3036';
    $dbuser = 'username';
    $dbpass = 'pw';
    $database= 'urbanalphabets';
    $table  = 'writings';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $db_found = mysql_select_db($database);

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

这是有效的代码:

    <?php
    ini_set('display_errors', 1);

//phpinfo();
    require '.ht-loginChangeLog.php';

    $myquery="SELECT ID, title, time, text, tag_iOS, tag_web, tag_android, tag_other FROM changeLog";
    $query=mysql_query($myquery);

    if(! $myquery){
        echo mysql_error();
        die;
    }

    $data=array();

    for($x=0; $x < mysql_num_rows($query); $x++){
        $data[]=mysql_fetch_assoc($query);
    }
    echo json_encode($data);


?>

.HT-loginChangeLog.php

<?php
    $dbhost = 'localhost:3036';
    $dbuser = 'username';
    $dbpass = 'pw';
    $database= 'urbanalphabets';
    $table  = 'changeLog';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $db_found = mysql_select_db($database);


if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

使用建议转换为utf-8的函数后,下面的ajax请求会抛出错误:

$.ajax ( {
       url: theURL,
       dataType: "json",
       success: function ( data, textStatus, jqXHR ) {
          console.log(data);
          console.log("sucess");
           receivedData=data;
           console.log(receivedData[0].ID, receivedData[0].title, receivedData[0].shortDescription, receivedData[0].text, receivedData[0].date, "length", receivedData.length);
           initialize();
       },
       error:function(  jqXHR,  textStatus,  errorThrown ){
                  console.log("failing");
                  console.log(jqXHR);
                  console.log(errorThrown);
                  console.log(textStatus);
       }
   } );

错误:

 Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
abort: function (e){var t=e||w;return u&&u.abort(t),k(0,t),this}
always: function (){return i.done(arguments).fail(arguments),this}
complete: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
done: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
error: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
fail: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
getAllResponseHeaders: function (){return 2===b?a:null}
getResponseHeader: function (e){var t;if(2===b){if(!c){c={};while(t=Tn.exec(a))c[t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}
overrideMimeType: function (e){return b||(p.mimeType=e),this}
pipe: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
progress: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
promise: function (e){return null!=e?x.extend(e,r):r}
readyState: 4
responseText: "14[{"ID":"17","title":"Christmas Mlab demo day","shortDescription":"first ever public presentation of the prototype","text":"Unfortunately I was not there myself so there is not much I can say about it.","date":"2012-12-19 14:10:26","referenceProject":"1","workshop":"1","otherTag":"1"},{"ID":"22","title":"the App was born","shortDescription":"During the Multitouch Interaction Course at Media Lab Helsinki the very first prototype of the Urban Alphabets App was programmed using Open Frameworks.","text":"","date":"2012-12-10 12:18:28","referenceProject":"0","workshop":"0","otherTag":"0"},{"ID":"34","title":"

此处有更多内容

","date":"2014-04-07 11:20:54","referenceProject":"0","workshop":"0","otherTag":"0"}]"
setRequestHeader: function (e,t){var n=e.toLowerCase();return b||(e=v[n]=v[n]||e,y[e]=t),this}
state: function (){return n}
status: 200
statusCode: function (e){var t;if(e)if(2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this}
statusText: "OK"
success: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
then: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
__proto__: Object
 projectBlog.php:122
SyntaxError {stack: (...), message: "Unexpected token ["}
message: "Unexpected token ["
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
 projectBlog.php:123
parsererror

1 个答案:

答案 0 :(得分:0)

嗯,经过很多评论后,这里是你的答案(最有可能)

Link

通常,它是json_encode的编码问题。你需要拥有UTF8中的所有内容。

您可以查看将字符串转换为UTF-8 here的好方法。

编辑:

我已经考虑过了。将数据库移动到新服务器时,您可能会意外地弄乱编码。它发生了一段时间。

您可以导出数据库转储,在某些程序中转换为UTF-8,如NotePad ++,然后将其还原回服务器。这是一个快速修复。

此外,您可能会考虑开始使用一些框架,例如CodeIgniter或Yii或Symphony,它们将为您完成大部分工作。编写纯PHP是很困难的并使用框架,其中有一些示例可能对您来说更容易,除非您正在尝试学习如何在PHP中编码。