Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter/PHP: Format db query as an array

    $this->db->select('id, user_id')->from('be_users')->where('id',$user_id);
    $data['user_individual'] = $this->db->get();

If this is my db query, how do I get an array of one db row...

ie. I want to do something like $data['user_individual']['id']->format_as_array...

like image 648
Kevin Brown Avatar asked Nov 27 '22 06:11

Kevin Brown


1 Answers

CodeIgniter provides several methods to perform on query results.

See here: https://codeigniter.com/user_guide/database/results.html

result() returns an array of PHP objects.

row() returns a single PHP object for that row.

result_array() returns an array of arrays.

row_array() returns a single array for that row.

row() and row_array() optionally take a parameter which is the number of the row you'd like to return.

Beyond this, it's difficult to tell exactly what you're asking for. You should be able to get your data out exactly as you like with these methods.

Edit:

Incidentally, the way to access these methods is through the query object returned from the $this->db->get() call:

$query = $this->db->get();

$rows = $query->result(); //array of objects
$rows_array = $query->result_array(); //array of arrays
$row = $query->row(); //single object
$row_array = $query->row_array(); //single array
like image 60
treeface Avatar answered Dec 16 '22 08:12

treeface