I am using the below function to get data from a mysql db
The code wrap from my mododel.php is:
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;
}
The DB structure cols : id, user_id, device, value
But its extracting only the last record. How can i get all the records in a array.
use result_array()
instead of row_array()
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() > 0) {
return $query->result_array();
}
}
return FALSE;
}
it will return all records. row_array()
return only one record
Ok, I'll refactor the code and modify where needed:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.