Php Codeigniter模型函数从mysql获取数据
我正在使用下面的函数从mysql数据库获取数据 my mododel.php中的代码包装是:Php Codeigniter模型函数从mysql获取数据,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在使用下面的函数从mysql数据库获取数据 my mododel.php中的代码包装是: function get_all_devices($user_id = NULL) { if ($user_id) { $sql = " SELECT * FROM {$this->_db} WHERE user_id = " . $this->db->escape($user_id) .
function get_all_devices($user_id = NULL) {
if ($user_id) {
$sql = "
SELECT *
FROM {$this->_db}
WHERE user_id = " . $this->db->escape($user_id) . "
";
$query = $this->db->query($sql);
if ($query->num_rows()) {
return $query->row_array();
}
}
return FALSE;
}
数据库结构cols:id、用户id、设备、值
但它只提取了最后一条记录。
如何获取数组中的所有记录。使用结果数组()
而不是行数组()
它将返回所有记录<代码>行数组()
只返回一条记录使用结果数组()
而不是行数组()
它将返回所有记录
row\u array()
只返回一条记录好的,我将重构代码并在需要时进行修改:
function get_all_devices($user_id = NULL) {
if ($user_id) {
$this->db->where('user_id', $user_id);// you don't have to escape `$user_id` value, since `$this->db->where()` escapes it implicitly.
$query = $this->db->get($this->_db); //executes `select *` on table `$this->_db`, and returns.
//if you want to get only specific columns, use $this->db->select('col1, col2'), otherwise you don't need to specify it, since it implicitly selects everything.
if ($query->num_rows()) {
return $query->result_array();//use result_array() to retrieve the whole result instead of row_array() which retrieves only one row;
}
}
return FALSE;
}
好的,我将重构代码并在需要时进行修改:
function get_all_devices($user_id = NULL) {
if ($user_id) {
$this->db->where('user_id', $user_id);// you don't have to escape `$user_id` value, since `$this->db->where()` escapes it implicitly.
$query = $this->db->get($this->_db); //executes `select *` on table `$this->_db`, and returns.
//if you want to get only specific columns, use $this->db->select('col1, col2'), otherwise you don't need to specify it, since it implicitly selects everything.
if ($query->num_rows()) {
return $query->result_array();//use result_array() to retrieve the whole result instead of row_array() which retrieves only one row;
}
}
return FALSE;
}