W3docs

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.

The $this->db->get() function in CodeIgniter (v3) 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(). Note that when filtering for a specific row, get_where() is commonly used as a shortcut for get() combined with where().

For example, to get the values for a specific user with the ID of 1, you can use the following code:

Example of retrieving data from a database table in Codeigniter

<?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.

To get the first row as an associative array instead of an object, use the row_array() function:

Example of using the row_array() function to return the first row as an array in Codeigniter

<?php

$query = $this->db->get_where('users', ['id' => 1]);
$row = $query->row_array();

In this case, the array keys will be the column names, allowing you to access values like $row['name'].