PHP试图获取非对象的属性 - 不确定原因

时间:2016-06-28 09:00:42

标签: php

我试图使用PDO和JQuery AJAX从数据库生成应付款清单的DataTable但是我在PHP文件上遇到这个错误。我不确定我的代码有什么问题。

错误消息显示:

<MyCompany_Coreextensions before="Mage_Wishlist">MyCompany_Coreextensions_Wishlist</MyCompany_Coreextensions>

pay_controller.php中的第121行是:

[28-Jun-2016 16:33:12 Asia/Manila] PHP Notice:  Trying to get property of non-object in ** on line 121

这是pay_controller.php

if($allPayList->res == 0) { echo ""; }

这是mod_pay.php

<?PHP
class PayController {

public function pay() {

    if (empty($_GET['sid']))
    return call('home', 'expire');

    $getSessionData = SessionData::getData($_GET['sid']);

    if($getSessionData->res = 0)
    {
        return call('home', 'expire');
    }
    date_default_timezone_set('Asia/Manila');
    $dtnow = date('Y-m-d H:i:s');
    $sexpire = date('Y-m-d H:i:s', strtotime($getSessionData->ssend));

    if($sexpire < $dtnow)
    {
        return call('home', 'expire');
    }

    $getMenu = MENU::getMenu();
    $menuid = 3;

    require_once('views/pay.php');

}

public function currentpay() {

    $curPayList = Payables::getCurrentPayable();

    echo "
        <div class='col-md-12'>
            <table id='currentpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if($curPayList->res == 0) {
            echo "";
        }
        elseif($curPayList->res == 1) {

            echo "<tr>
                    <td>" . $curPayList->seq . "</td>
                    <td>" . $curPayList->pdate . "</td>
                    <td>" . $curPayList->vendor . "</td>
                    <td>" . $curPayList->refno . "</td>
                    <td class='text-right'>" . number_format($curPayList->amount,2) . "</td>
                    <td>
                        <a OnClick='showCurPayDetails(". $curPayList->seq .")' class='btn btn-sm btn-default'>
                        <span class='fa-stack fa-fw'>
                          <i class='fa fa-square-o fa-stack-2x'></i>
                          <i class='fa fa-eye fa-stack-1x'></i>
                        </span>
                        VIEW DETAILS
                    </a>
                    </td>
                </tr>";
        }
        elseif($curPayList->res > 1) {
            foreach($curPayList as $cPL) {
                echo "<tr>
                        <td>" . $cPL->seq . "</td>
                        <td>" . $cPL->pdate . "</td>
                        <td>" . $cPL->vendor . "</td>
                        <td>" . $cPL->refno . "</td>
                        <td class='text-right'>" . number_format($cPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $cPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }

            echo "
                </tbody>
            </table>
        </div>
        ";
}

public function allpay() {

    $allPayListing = Payables::getAllPurchase();

    echo "
        <div class='col-md-12'>
            <table id='allpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if($allPayListing->res == 0) {

            echo "";

        }
        elseif($allPayListing->res == 1) {
            echo "<tr>
                    <td>" . $allPayList->seq . "</td>
                    <td>" . $allPayList->pdate . "</td>
                    <td>" . $allPayList->vendor . "</td>
                    <td>" . $allPayList->refno . "</td>
                    <td class='text-right'>" . number_format($allPayList->amount,2) . "</td>
                    <td>
                        <a OnClick='showCurPayDetails(". $allPayList->seq .")' class='btn btn-sm btn-default'>
                        <span class='fa-stack fa-fw'>
                          <i class='fa fa-square-o fa-stack-2x'></i>
                          <i class='fa fa-eye fa-stack-1x'></i>
                        </span>
                        VIEW DETAILS
                    </a>
                    </td>
                </tr>";
        }
        elseif($allPayListing->res > 1) {
            foreach($allPayListing as $aPL) {
                echo "<tr>
                        <td>" . $aPL->seq . "</td>
                        <td>" . $aPL->pdate . "</td>
                        <td>" . $aPL->vendor . "</td>
                        <td>" . $aPL->refno . "</td>
                        <td class='text-right'>" . number_format($aPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $aPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }

    echo "
                </tbody>
            </table>
        </div>
        ";
}

public function error() {

    require_once('404.php');

}

public function expire(){

    require_once('expire.php');
 }
}
?>

注意:我正在复制不会返回错误的<?PHP class Payables { public $seq; public $pdate; public $vendor; public $refno; public $amount; public $res; public function __construct($seq, $pdate, $vendor, $refno, $amount, $res) { $this ->seq = $seq; $this ->pdate = $pdate; $this ->vendor = $vendor; $this ->refno = $refno; $this ->amount = $amount; $this ->res = $res; } public static function getCurrentPayable() { $db = Db::getInstance(); $currentPay = $db->prepare('SELECT payables.seq AS seq, payables.purdate AS pdate, companies.compname AS vendor, payables.refno AS refno, payables.balance AS amount FROM payables LEFT JOIN companies ON companies.seq = payables.vendor WHERE payables.paidstatus = 0 '); switch($currentPay->execute()) { case True: $currentPayResCount = $currentPay->rowCount(); if($currentPayResCount == 0) { $currentPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0); } elseif($currentPayResCount == 1) { $currentPayRes = $currentPay->fetch(); $currentPayList = new Payables($currentPayRes['seq'], $currentPayRes['pdate'], $currentPayRes['vendor'], $currentPayRes['refno'], $currentPayRes['amount'], $currentPayResCount); } elseif($currentPayResCount > 1) { $currentPayRes = $currentPay->fetchAll(); foreach($currentPayRes as $cPR) { $currentPayList[] = new Payables($cPR['seq'], $cPR['pdate'], $cPR['vendor'], $cPR['refno'], $cPR['amount'], $currentPayResCount); } } return $currentPayList; break; case False: $currentPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0); return $currentPayList; break; } } public static function getAllPurchase() { $db = Db::getInstance(); $allPayables = $db->prepare('SELECT payables.seq AS seq, payables.purdate AS pdate, companies.compname AS vendor, payables.refno AS refno, payables.balance AS amount FROM payables LEFT JOIN companies ON companies.seq = payables.vendor '); switch($allPayables->execute()) { case True: $allPayResCount = $allPayables->rowCount(); if($allPayResCount == 0) { $allPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0); } elseif($allPayResCount == 1) { $allPayRes = $allPayables->fetch(); $allPayList = new Payables($allPayRes['seq'], $allPayRes['pdate'], $allPayRes['vendor'], $allPayRes['refno'], $allPayRes['amount'], $allPayResCount); } elseif($allPayResCount > 1) { $allPayRes = $allPayables->fetchAll(); foreach($allPayRes as $aPR) { $allPayList[] = new Payables($aPR['seq'], $aPR['pdate'], $aPR['vendor'], $aPR['refno'], $aPR['amount'], $allPayResCount); } } return $allPayList; break; case False: $allPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0); return $allPayList; break; } } } ?> 函数。它返回1个结果。 getCurrentPayable()应该返回2。

编辑:我收录了整个文件。 编辑:我编辑它以反映我最近所做的更改,它仍然显示相同的错误。

6 个答案:

答案 0 :(得分:1)

因此,对于那些想知道的人来说,这个问题的正确答案和问题的解决方案是在&#39; mod_pay.php&#39;中使用了多个条件之一。初始化相关对象的文件实际上是返回这些对象的数组,因此无法使用if($allPayList->res == 0) { echo ""; }直接作为对象进行访问。

在不了解系统意图的情况下,我无法真正提供最合适的方法来处理从数据库返回的多个记录。

答案 1 :(得分:0)

可能您正在获取的数据不存在于连接表中。一定要从提取中跳过该数据,否则添加&#34; ON DELETE CASCADE&#34;或&#34; ON UPDATE SET NULL&#34;在数据表中应用引用键时选项。

答案 2 :(得分:0)

正如@arkascha所问,我们需要在某些情况下看到我猜测的函数getAllPurchase()可以返回null或其他一些非对象结果。因为它的返回一个对象,所以你不能调用从这个方法返回的任何属性或方法,存储在$allPayList的情况下,这就是PHP告诉你的原因。它的错误信息。

轻松&#34;修复&#34;是使用$allPayList->resisset($allPayList->res)!empty($allPayList->res)进行测试,如果其中任何一个返回true,则只能实际调用$allPayList->res

更好的解决方法是确保您更好地返回getAllPurchase()中有意义的内容,即返回对象的空实例,以便始终有res属性可用且它为&#39 ; 可能是truefalse或可能是null

答案 3 :(得分:0)

$ allPayList是一个ARRAY,你将它用作对象。

首先使用print_r()方法检查$ allPayList中的所有变量。 然后你会很容易找到数组的层次结构。

答案 4 :(得分:0)

尝试使用适当的方法从Payables类getAllPurchase()方法返回值。 在您的代码中,静态方法getAllPurchase()在某些情况下返回Payables类的对象(实例)&amp;在其他情况下,它返回应付款对象数组。

因此,使用一种方法来检查$ allPayListing变量中的返回值是一个对象数组还是Payables类的一个对象:

使用以下代码绕过第121行出现的错误:

    if(!is_array($allPayListing))
    {
        if(isset($allPayListing->res) && $allPayListing->res == 0) {
            echo "";    
        }
    }

答案 5 :(得分:0)

我终于解决了它。感谢您的帮助。

事实证明,由于getAllPurchase()函数返回2条记录,因此条件会创建一个对象数组。由于它是一个数组,因此不能将其作为对象读取。

第一个函数getCurrentPayable()函数只返回1个记录,因此条件会创建一个对象。

这是更新的工作代码。

public function allpay() {

    $allPayListing = Payables::getAllPurchase();

    echo "
        <div class='col-md-12'>
            <table id='allpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if(!is_array($allPayListing)) {
            if(isset($allPayListing->res) && $allPayListing->res == 0) {
                echo "";    
            }
            else {

                echo "<tr>
                        <td>" . $allPayList->seq . "</td>
                        <td>" . $allPayList->pdate . "</td>
                        <td>" . $allPayList->vendor . "</td>
                        <td>" . $allPayList->refno . "</td>
                        <td class='text-right'>" . number_format($allPayList->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $allPayList->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }
        else {

            foreach($allPayListing as $aPL) {
                echo "<tr>
                        <td>" . $aPL->seq . "</td>
                        <td>" . $aPL->pdate . "</td>
                        <td>" . $aPL->vendor . "</td>
                        <td>" . $aPL->refno . "</td>
                        <td class='text-right'>" . number_format($aPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $aPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }

        }

    echo "
                </tbody>
            </table>
        </div>
        ";
}

我首先计算了$allPayListing的内容然后抛出条件,如果它大于0或1则表示它是一个数组。我必须先遍历数组才能访问对象。