使用以下函数,我试图从数据库中提取数据(如有必要),并使用OOP PHP在视图页面上显示结果。此函数从DB中提取数据:
function getdata () {
$con = mysql_connect("localhost","root","XXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user", $con);
$sql="SELECT major FROM Education WHERE id=1";
$result=mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
mysql_close($con);
}
因此,上一个函数从数据库中获取活动用户的大学专业。只有在设置了该值时才会调用它,如下所示:
function recommender () {
if (isset($this->collegemajor)) {
echo "Your major is on Facebook. You're majoring in $this->collegemajor.";
} else {
$this->getdata();
echo "Your major is in the DB. You're majoring in $this->collegemajor_db";
}
}
collegemajor_db的值设置如下:
$collegemajor = isset($user_profile['education'][0]['concentration'][0]['name']) ? $user_profile['education'][0]['concentration'][0]['name'] : null ;
$collegemajor_db = isset($row['major']) ? $row['major'] : null ;
$profile = new profile ($collegemajor, $collegemajor_db);
$profile->recommender();
出于某种原因,当我运行此代码时,$ this-> collegemajor_db不会填充。数据库连接有效,我只是在设置对象属性collegemajor_db的值时遇到问题(我猜)。
答案 0 :(得分:0)
return $row;
或return $result;
。就像我说的,我可能是错的,但我认为这可能是问题。