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.
$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());
}
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'));
}
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());
}
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());
}
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());
}
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());
}
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());
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in