Codeigniter $this->db->get(), how do I return values for a specific row?
The $this->db->get()
function in CodeIgniter is used to retrieve data from a database table. To return values for a specific row, you can use the row()
function after calling $this->db->get()
.
For example, to get the values for a specific user with the ID of 1, you can use the following code:
<?php
$query = $this->db->get_where('users', ['id' => 1]);
$row = $query->row();
This will return an object with the values for the row where the 'id' column is equal to 1 in the 'users' table. You can then access the values of the row by using the column names as properties of the object, for example $row->name
to get the name of the user.
Alternatively, you can use the first_row()
function to directly return the first row.
<?php
$query = $this->db->get_where('users', ['id' => 1]);
$row = $query->first_row();
It is also possible to use the result_array()
function to return the result as an array, in this case the array key will be the column name, instead of object property.
<?php
$query = $this->db->get_where('users', ['id' => 1]);
$row = $query->row_array();
You can also use the row_array()
function to get the first row as an array.
<?php
$query = $this->db->get_where('users', ['id' => 1]);
$row = $query->row_array();