PHP注意:未定义的属性

时间:2015-09-14 07:53:34

标签: php class

这是一个php类。

class WADB{
    private $sDbHost;
    private $sDbName;
    private $sDbUser;
    private $sDbPwd;
    private $iNoOfRecords;
    private $oQueryResult;
    private $aSelectRecords;
    private $aArrRec;
    private $bInsertRecords;
    private $iInsertRecId;

    function __construct($sDbHost, $sDbName, $sDbUser, $sDbPwd){
        // link to DB...
    }

    function selectRecords ($sSqlQuery){
        unset($this->aSelectRecords);
        $this->oQueryResult = mysql_query($sSqlQuery) or die(mysql_error());
        $this->iNoOfRecords = mysql_num_rows($this->oQueryResult);
        if ($this->iNoOfRecords > 0) {
            while ($oRow = mysql_fetch_array($this->oQueryResult,MYSQL_ASSOC)) {
                $this->aSelectRecords[] = $oRow;
            }
            mysql_free_result($this->oQueryResult);
        }
        $this->aArrRec = $this->aSelectRecords;
        return array(   'data'      => $this->aArrRec,
                        'record'    => $this->iNoOfRecords);
    }
}

这就是我使用它的方式:

require_once('WADB.php');
$db = new WADB('{HOST}','{DB_NAME}','{User}','{Password');    
$s1 = "SELECT * FROM {TableName} WHERE UserId='$_POST[UserId]';";
$d1 = $db->selectRecords($s1);

在我使用它之前没有显示通知,但现在显示

  

注意:未定义的属性: {my_computer_url} \ WADB.php 中的WADB :: $ aSelectRecords 31 < / p>

第31行是:

$this->aArrRec = $this->aSelectRecords;

之前没有显示我使用它。 我不知道为什么......?!

-

抱歉我的英语不好,希望你明白我的意思。 ='(

2 个答案:

答案 0 :(得分:1)

selectRecords方法中,您有unset($this->aSelectRecords);,这有效地取消了aSelectRecords数据成员的设置。因此,当您稍后尝试访问数据时,它将不再“可用”。 如果您要做的是清空此变量的数组,则不要使用unset,而是执行此操作:

$this->aSelectRecords= array();

所以你将方法定义为:

function selectRecords ($sSqlQuery){
    $this->aSelectRecords = array(); //rather than `unset`
    $this->oQueryResult = mysql_query($sSqlQuery) or die(mysql_error());
    $this->iNoOfRecords = mysql_num_rows($this->oQueryResult);
    if ($this->iNoOfRecords > 0) {
        while ($oRow = mysql_fetch_array($this->oQueryResult,MYSQL_ASSOC)) {
            $this->aSelectRecords[] = $oRow;
        }
        mysql_free_result($this->oQueryResult);
    }
    $this->aArrRec = $this->aSelectRecords;
    return array(   'data'      => $this->aArrRec,
                    'record'    => $this->iNoOfRecords);
}

答案 1 :(得分:0)

这里你没有设定unset($this->aSelectRecords); 完成此移动后,您有if()if(false) 你试图获得这个属性的价值,但他的未设置 而且你得到错误。