Codeigniter / PHP: format a db request 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 request, how do I get an array from a single db string ...

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

+4
source share
4 answers

CodeIgniter provides several methods for executing query results.

See here: https://ellislab.com/codeigniter/user-guide/database/results.html

result() returns an array of PHP objects.

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

result_array() returns an array of arrays.

row_array() returns a single array for this row.

row() and row_array() do not have to specify a parameter, which is the number of the row you want to return.

Other than that, it's hard to say exactly what you are asking for. You should be able to receive data exactly the way you like using these methods.

Edit

By the way, the way to access these methods is through the request object returned by calling $this->db->get() :

 $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 
+30
source

When you use $this->db->get(); , you can use $this->db->result(); which returns an array.

 $query = $this->db->get('table_name'); foreach ($query->result() as $row) { echo $row->title; } 

See http://codeigniter.com/user_guide/database/results.html for details on how to retrieve results from a database.

+1
source

Instead of $ this-> db-> get () use $ this-> db-> row_array ();

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

/ ** * format_database_array * * Allows you to format database object data into an array

  • @access public
  • @param String $ db_object: database query object
  • @return String: returns an array with data * /

if (! function_exists ('format_database_array')) {

  function format_database_array($db_object) { $db_array = array(); if($db_object-> num_rows >0) { foreach ($db_object->result_array() as $row) { foreach ($row as $key => $value) { $db_array[$key] = $value; } } } return $db_array; } 

}

0
source

Source: https://habr.com/ru/post/1334440/


All Articles