php - Codeigniter -> 提取更多数据

标签 php mysql model-view-controller codeigniter

我有以下代码来确认数据库的激活。我现在想要的是什么是让我能够提取链接到特定激活码的用户名 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/

相关文章:

php - 是否可以将 wordpress 评论框作为必填字段?

php - 自引用表和递归 SQL 函数

mysql - 带有 MySQL 和 JDBC 的多个语句的触发器语法异常

mysql - 读取 MySQL 数据库中存储为 BLOB 的 Word DOC

php - 使用 jQuery/javascript 代码时 PHP MVC 应该是什么样子?

javascript mvc 和 ajax 表单提交

php - 超越 CRUD 的锂应用程序

php - 从具有不同字段和唯一值的两个表中获取值

javascript - Ext.JS - 网格不加载存储值

php - 如何强制 cURL 发送完整的 URL 作为请求目标?