JSON数据表错误

时间:2013-09-24 20:30:37

标签: php json

首先,让我指出我不知道JSON是如何工作的。我绝不是一个强大的程序员。当我将!=更改为==,其中显示$sWhere .= "status == '".APS_DONE."'";时出现JSON解析错误:

  

“DataTables警告:无法解析来自服务器的JSON数据。这   是由JSON格式错误引起的。“

关于这意味着什么的任何线索。

<?php
include('aps2.database.php');
include('aps2.login.php');

*********** COMPLETED REQUEST PROCESSING CODE



$sTable = 'aps2_requests';

$aColumns = array( 'rid', 'description', 'creatoruid', 'photoguid');
$sIndexColumn = 'description';

$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
    $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
        mysql_real_escape_string( $_GET['iDisplayLength'] );
}

if ( isset( $_GET['iSortCol_0'] ) )
{
    $sOrder = "ORDER BY  ";
    for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
    {
        if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
        {
            $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
        }
    }

    $sOrder = substr_replace( $sOrder, "", -2 );
    if ( $sOrder == "ORDER BY" )
    {
        $sOrder = "";
    }
}

$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}

if(strlen($sWhere) > 1) 
{
    $sWhere .= " AND ";
}
else
{
    $sWhere = "WHERE ";
}

$sWhere .= "status == '".APS_DONE."'";

/*
 * SQL queries
 * Get data to display
 */
$sQuery = "
    SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
    FROM   $sTable
    $sWhere
    $sOrder
    $sLimit
";
$rResult = mysql_query( $sQuery ) or die(mysql_error());

/* Data set length after filtering */
$sQuery = "
    SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
    SELECT COUNT(".$sIndexColumn.")
    FROM   $sTable
";

$rResultTotal = mysql_query( $sQuery ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];

/*
 * Output
 */
$output = array(
    "sEcho" => intval($_GET['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array()
);

while ( $aRow = mysql_fetch_array( $rResult ) )
{
    $row = array();
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $aColumns[$i] == "version" )
        {
            $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
        }
        else if ( $aColumns[$i] != ' ' )
        {
            if ($aColumns[$i] == 'creatoruid')
            {
                $row[] = '<p style="margin: 5px;">' . aps2_user_name_from_uid($aRow[$aColumns[$i]]) . '</p>';
            } 
            else if ($aColumns[$i] == 'photoguid')
            {
                $row[] = '<p style="margin: 5px;">' . aps2_get_request_photographer_or_status($aRow['rid']) . '</p>';
            }
            else if ($aColumns[$i] == 'description')
            {
                $row[] = '<p style="margin: 5px 10px;line-height: 1.2em;">' . stripslashes(preview_text_truncate($aRow[ $aColumns[$i] ])) . '</p>';
            } 
            else
            {
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
    }

    $status = aps2_get_request_status($aRow['rid']);


    $actionButtons = '<a class="actionbutton greenlink viewbutton" href="' . $aRow['rid'] . '">View</a>';
    $actionButtons .= '<a class="actionbutton greenlink uploadbutton" href="' . $aRow['rid'] . '">Upload</a>';


    if (($status == APS_UNASSIGNED || $status == APS_IN_PROGRESS) && aps2_get_user_role($_SESSION['uid']) > APS_STANDARD)
    {
        $actionButtons .= '<a class="actionbutton greenlink acceptbutton" href="' . $aRow['rid'] . '">Accept</a>';
    }

    if (($aRow['creatoruid'] == $_SESSION['uid'] && aps2_get_request_status($aRow['rid']) == APS_UNASSIGNED) || $_SESSION['uid'] == 1)
    {
        $actionButtons .= '<a class="actionbutton greenlink deletebutton" href="' . $aRow['rid'] . '">Delete</a>';
    }

    $row[] = '<div style="margin:3px 3px 6px 3px;text-align:center;">'.$actionButtons.'</div>';
    $output['aaData'][] = $row;
}

echo json_encode( $output );
?>

1 个答案:

答案 0 :(得分:1)

此代码生成SQL代码然后执行它。

当你有$sWhere .= "status != '".APS_DONE."'";时,它会查找状态不等于APS_DONE的表行。

MySQL(see the list for yourself)中没有==运算符,因此当您替换!= by ==时,您将获得无效的SQL代码。

我猜这会输出一条MySQL错误消息,该消息无效JSON。

使用=代替==