getResult , getResultArray , getRow, getRowArray in codeigniter - 4

Deepak Tailor Image
Deepak Tailor - Oct 17 2020
getResult , getResultArray , getRow, getRowArray in codeigniter - 4

There are several ways in Codeigniter 4 to generate query results. Such as for object format getResult(), for array getResultArray(),for single row in object getRows(),for single row in array getRowArray() functions etc.

getResult() function
The getResult() function returns data to us in the object format.
$this->seller  // database connection set in constructor 
public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getResult());
}
getResultArray() function

This method returns the query result as a pure array, or an empty array when no result is produced.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getResult('array')); // return result in array
}

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getResultArray('array'));
}
getRow() function

If we have more than one rows in our query and we need only one row, then we use this getRow function.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getRow());
}
getRowArray() function

If our query has more than one rows and we want the same single row array format, then we use this getRowArray function.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getRowArray());
}
getFirstRow() function

If first row is needed from our query, then we use this getFirstRow function.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getFirstRow());
}
getLastRow() function

If we want the last row from our query, then we use this getLastRow function.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getLastRow());
}
getFieldCount() and getFieldNames() function

If we want to count the fields in a table or fetch the name of the fields, we use the getFieldCount() and getFieldNames() functions.

public function get_all_data()
{
	$query = $this->seller->query('SELECT * FROM users');
	echo "<pre>";
	print_r($query->getFieldCount());
	echo "<br/>";
	print_r($query->getFieldNames());
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry for the last 5 years. For professional and customize web development, you can send inquiry on our email.
----
You can contact him at support@fulstackdev.com