php - CodeIgniter 模型无法返回特定记录

标签 php mysql codeigniter model rows

我正在使用 CodeIgniter,但在使用 results() 方法从表。

这是我的模型:

public function get_all_entries($id)
    {
        // Select row to be fetched
        $this->db->where('id', $id);
        $this->db->get('users');
        // Execute the find query with provided data
        $query = $this->db->get('users');
        // Return an object with all the data
        return $query->result();
    }

它应该返回 users 表中与 $id 参数匹配的所有行,但相反,它只是获取表中的所有记录,包括与提供的 $id 参数不匹配的那些。

我在这里做错了什么?我已经尝试了 row() 方法,虽然它与 where() 一起工作,但它只返回一行,所以它不适合我的情况。

最佳答案

问题是您调用了两次 get() 方法,第一次调用 where,但没有分配给变量;第二个被分配给一个变量,但由于 where 子句已经被另一个使用,它得到了一切。删除第一个 get,你应该没问题。

public function get_all_entries($id)
{
    // Select row to be fetched
    $this->db->where('id', $id);
    // Execute the find query with provided data
    $query = $this->db->get('users');
    // Return an object with all the data
    return $query->result();
}

关于php - CodeIgniter 模型无法返回特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049065/

相关文章:

javascript - 使用 Ajax 显示数据库中的数据

mysql - MySQL:从另一个表中查找服务器

php - 如何在 CodeIgniter 中使动态变量值全局化?

php - MySQL的列查找模式

javascript - 错误未显示在 ajax 提交页面上

php - NuSOAP PHP 网络服务

php - 使用 sha256_password(访问被拒绝)时如何使用 PHP 的 mysqli 连接到 MySQL

mysql - MySQL,在SELECT结果中对行进行分组

javascript - 加载资源失败 : The server responded with a status of 404 (not found)

php - mysql函数划分列的总和值和