我有以下代码来确认数据库的激活。我现在想要的是什么是让我能够提取链接到特定激活码的用户名 userFirstName
的最佳方法?以及如何将其作为 $data['userFirstName'] = What
传递到 Controller 中。
型号:
function confirmUser($activateCode)
{
if($activateCode == '')
{
return FALSE;
}
//Selects the userID where the given URI activateCode = ?
$this->db->select('userID');
$this->db->from('users');
$this->db->where('userActiveCode', $activateCode);
$result = $this->db->get();
if($result->num_rows == 1) // If the above result is = 1 then update the userActive row else it will fail
{
$this->db->set('userActive', 1);
$this->db->where('userActiveCode', $activateCode);
$this->db->update('users');
return TRUE;
}else{
return FALSE;
}
}
最佳答案
您还可以使用模型方法的引用参数将用户的名字返回给调用者。这将允许一个干净的 bool 返回值。
型号
function confirmUser($activateCode, &$userFirstName)
{
if($activateCode == '')
return false;
$this->db->select('userID, userFirstName');
$this->db->from('users');
$this->db->where('userActiveCode', $activateCode);
$result = $this->db->get();
if($result->num_rows == 1)
{
$this->db->set('userActive', 1);
$this->db->where('userActiveCode', $activateCode);
$this->db->update('users');
$userFirstName = $result->row()->userFirstName;
return true;
}
else
return false;
}
来电者
$userFirstName = '';
if ($this->user_model->confirmUser($activate_code, $userFirstName))
//userFirstName will be populated
$data['userFirstName'] = $userFirstName;
else
//userFirstName will still be empty
当按引用传递时,与按值传递相反,您实际上是将指针(内存地址)传递到内存中保存值而不是实际值的位置;因此,当您更改方法中的值时,调用者通过引用传递的参数也会更改。这是因为它们都指向相同的内存地址。
关于php - Codeigniter -> 提取更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578949/