从MySQL数据库获取数据时未定义的属性

时间:2012-07-31 06:07:25

标签: php mysql sql select

我尝试使用此脚本获取数据:

   public function getReligion()
    {
        $sql = 'select refReligionId, refReligionNameEN FROM ref_religion group by refReligionNameEN';
        $this->selectSql($sql);             
        $results = $this->getResult();              
        $forms = '
        <select name="slcReligion" id="slcReligion" style="width: 204px">
            <option value="" selected>Select Religion</option>
        ';              
        foreach($results as $result)
        {
            $forms .= '<option value="'.$result->refReligionId.'">'.$result->refReligionNameID.'</option>';
        }
        $forms .= '</select>';              
        return $forms;
    }

我有父类函数:

    public function selectSql($sql)
{
    $query = @mysql_query($sql);
    if($query)
    {
        $this->numResults = mysql_num_rows($query);
        for($i = 0; $i < $this->numResults; $i++)
        {
            $r = mysql_fetch_array($query);
            $key = array_keys($r);
            for($x = 0; $x < count($key); $x++)
            {
                // Sanitizes keys so only alphavalues are allowed
                if(!is_int($key[$x]))
                {
                    if(mysql_num_rows($query) > 1)
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    else if(mysql_num_rows($query) < 1)
                        $this->result = null;
                    else
                        $this->result[$key[$x]] = $r[$key[$x]];
                }
            }
        }       
        return true;
    }
    else
    {
        return false;
    }
}   

Abd get result function:

    public function getResult($getArray = false)
{
    $encode = json_encode($this->result);

    if($getArray == true)
        $array = true;
    else
        $array = false;                                 

    if($this->numResults == 1)
        $results = '['.$encode.']'; 
     else
        $results = $encode;

    $result = json_decode($results, $array);

    return $result;
}

我可以使用这些脚本获取数据,

问题是php页面出现了这些错误:

Undefined property: stdClass::$refReligionId
Undefined property: stdClass::$refReligionNameID

1 个答案:

答案 0 :(得分:0)

您选择refReligionNameEN,但在对象引用中使用refReligionNameIDENID)。