php - 如果表为空,如何处理 json 格式错误显示

标签 php jquery mysql zend-framework

我正在从表中获取值,它工作得很好。当表为空时,我收到警报框 json 格式错误。我该如何处理该错误?

我的sql查询如下,我使用php zend框架

public function getProductsAction(){

     $oProductModel = new Application_Model_Db_Table_Products();
     $oSelect = $oProductModel->fetchAllProductItems();

     echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));

}

查看页面是

$(document).ready(function(){

    jQuery('#product-table').dataTable({
        <?php if(Qsm_User::isAdmin()){ ?>
            "sDom": 'Tlfrtip',
            "oTableTools": {
                "sSwfPath": "/js/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf",
                "aButtons": [{
                        "sExtends":    "collection",
                        "sButtonText": 'Save <span class="caret" />',
                        "aButtons":    [ "csv", "xls", "pdf" ]
                    }]
            },
       <?php } ?>
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bServerSide": false,
        "sAjaxSource": '/buyproduct/api/get-products',

        "aoColumns": [  
            {"sTitle":"ID", "bVisible":false}, 
            {"sTitle":"Emp Name", "bVisible":true},
            {"sTitle":"Product", "sWidth": '10%'},
            {"sTitle":"Brand", "sWidth": '10%'},
            {"sTitle":"Condition", "sWidth": '10%'},
            {"sTitle":"Description", "sWidth": '20%'},
            {"sTitle":"Image","sWidth": '18%',
             "mData": null,
             "bSearchable": false, 
             "bSortable": false,
             "fnRender": function (oObj){
            var actions = "";
            actions += "<span class=''>\n\
             <img src='/uploads/gallery/" + oObj.aData[6] + "' alt='Image' title='Image' class='style_prevu_kit' width='0px' height='0px'  />";
                actions +="</span>";
            return actions;
            }
            },
            {"sTitle":"Starting Bid Price", "sWidth": '10%'},
            {"sTitle":"Current Bid Price", "sWidth": '10%'},
            {
                "sTitle":"Bid",
                "sWidth": '17%',
                "mData": null,
                "bSearchable": false,                               
                "bSortable": false,
                "fnRender": function (oObj){
                    var actions = "";
                    actions += "<span class='data_actions iconsweet'>\n\
                               <a title='Buy' href='/buyproduct/index/bid/id/" + oObj.aData[0] + "'><img src='/images/buy.png' alt='Buy'  title='Buy' /></a>";
                    actions +="</span>";
                    return actions;
                 }
            }
            <?php if(Qsm_User::isAdmin()){ ?>
            ,{
                "sTitle":"Transaction",
                "sWidth": '22%',
                "mData": null,
                "bSearchable": false,
                "bSortable": false,
                "fnRender": function (oObj){
                    var actions1 = "";
                    actions1 += "<span class='data_actions iconsweet'>\n\
                                <a title='Transaction' href='/buyproduct/index/transaction/id/" + oObj.aData[0] + "'><img src='/images/icons/edit3.png' alt='Edit' title='Transaction' /></a>";

                    actions1 +="</span>";
                    return actions1;
                }
            }
            <?php } ?>
        ]
    });     

});

请有人帮我处理这个 json 格式错误

最佳答案

如果没有行条件,您的操作将返回空字符串,这是无效的 json 数据,请检查返回的值

$this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount'))

如果为空/或没有行回显类似这样的内容或空行数据

{
   "msg": "no-data"
}

更新:

public function getProductsAction(){

 $oProductModel = new Application_Model_Db_Table_Products();
 $oSelect = $oProductModel->fetchAllProductItems();
 if($oSelect){
    echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));
 }else{
    echo '{"msg": "no-data"}';  //or edit with empty data matching required by your view
 }
}

关于php - 如果表为空,如何处理 json 格式错误显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41983225/

相关文章:

jquery - 背景覆盖+视差

php - 有没有更好的方法从 MySQL 数据库获取项目?

php - 通过 PHP 将 MySQL 表中的数据转换为 XML

mysql - 如何使用cte删除mysql中的重复数据

php - 没有输出的时候ob_start就没用了吗?

javascript - 无法使用 Angular 将数据 POST 到 Laravel 5 Controller ,错误 : 422 Unprocessable Entity

php - 从 php 和 mySQL 中的 select 语句中选择

jquery - 使用 AJAX 加载 Google 图表

javascript - 将 jQuery 函数应用于不同的元素以设置尺寸

php - Symfony 进程 - 从服务执行命令