我有一个宠物数据库,基本上我想通过它的 ID 查看宠物的详细信息。这是我的 Controller 方法的样子:
public function details()
{
$this->load->model('animalsmodel');
$row = $this->animalsmodel->details($this->uri->segment(3));
$this->load->view('shared/header');
$this->load->view('animals/details', $row);
$this->load->view('shared/footer');
}
这是AnimalsModel
获取相关行的代码:
function details($animalId) {
$q = $this->db->query('SELECT Animals.Name, Animals.DateAdmitted, Animals.FoundDescription, Animals.Description, Animals.Neutered, Animals.DateNeutered, Types.Name AS Type, Healthchecks.CheckInfo FROM Animals LEFT JOIN Types ON Animals.TypeId = Types.TypeId LEFT JOIN Healthchecks ON Animals.HealthcheckId = Healthchecks.HealthcheckId WHERE Animals.AnimalId = ?', $animalId);
if ($q->num_rows() > 0)
{
$row = $q->row();
return $row;
} else
{
echo "No Results Man!!";
}
}
我已经在 phpMyAdmin 中手动运行了那个 MySQL 查询,它可以工作,它让我得到了正确的行。
编辑:我刚刚var_dump()
在 $row 对象上做了一个,我得到了以下信息:
object(stdClass)#17 (8) { ["Name"]=> string(6) "Quemby" ["DateAdmitted"]=> string(10) "2013-01-28" ["FoundDescription"]=> string(94) "The story of how I got to be here. Phasellus ornare. Fusce mollis. Duis sit amet diam eu dolor" ["Description"]=> string(65) "massa non ante bibendum ullamcorper. Duis cursus, diam at pretium" ["Neutered"]=> string(1) "0" ["DateNeutered"]=> string(10) "0000-00-00" ["Type"]=> string(3) "Dog" ["CheckInfo"]=> string(26) "a, facilisis non, bibendum" }
所以看起来我有我的行!但是为什么CI一直在抱怨Undefined variable: row
:(