How To Get Single Row Data in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
233 Views

Inside this article we will see How to get single row data in CodeIgniter 4 application. Article contains very classified information about CodeIgniter 4 methods which helps to get single row data from database.

If you are looking for an article which makes you understand about how to get a single row from table then you are at the right place to learn that.

In CodeIgniter 4, there are several methods available to get single row data. Also selecting data rows from database in codeigniter 4, we have several options available as – Using Models, Using Query Builder, Using raw queries.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Database Connectivity to Application

Open .env file from project root. Search for DATABASE. You should see the connection environment variables.

Let’s set the value for those to connect with database.

 
#--------------------------------------------------------------------
# DATABASE
#-------------------------------------------------------------------- 
 database.default.hostname = localhost
 database.default.database = codeigniter4_app
 database.default.username = root
 database.default.password = root
 database.default.DBDriver = MySQLi
   

Now, database successfully connected with application.


Idea #1 – Working with Raw Queries

Suppose you have any controller and used raw query to get single row data.

Here, we have considered several cases to get single row

  • Single Row Data (Object Format)
  • Single Row Data in Array Format
  • First Single Row
  • Last Single Row
<?php

namespace App\Controllers;

class Site extends BaseController
{
    private $db;
    
    public function __construct()
    {
        $this->db = db_connect();
    }

    // get single row
    public function getUsers()
    {
        $query = $this->db->query("SELECT * from users");

        echo "<pre>";
        print_r($query->getRow());
    }
}

Single row result in Object format

    // get single row
    public function getUsers()
    {
        $query = $this->db->query("SELECT * from users");

        echo "<pre>";
        print_r($query->getRow()); // OR print_r($query->getRowObject());
    }

Single row result in Array format

    // get single row
    public function getUsers()
    {
        $query = $this->db->query("SELECT * from users");

        echo "<pre>";
        print_r($query->getRowArray());
    }

First row from table

    // get single row
    public function getUsers()
    {
        $query = $this->db->query("SELECT * from users");

        echo "<pre>";
        print_r($query->getFirstRow());
    }

Last row from table

    // get single row
    public function getUsers()
    {
        $query = $this->db->query("SELECT * from users");

        echo "<pre>";
        print_r($query->getLastRow());
    }

Idea #2 – Working with Query Builder

Suppose you have any controller and used query builder concept to get single row data.

<?php

namespace App\Controllers;

class Site extends BaseController
{
    private $db;
    
    public function __construct()
    {
        $this->db = db_connect();
    }

    public function getUsers()
    {
        $usersTable = $this->db->table("users");
        $usersTable->select("name, email");
        $query = $usersTable->get();

        echo "<pre>";
        print_r($query->getRow());
    }
}

Single row in object format

public function getUsers()
    {
        $usersTable = $this->db->table("users");
        $usersTable->select("name, email");
        $query = $usersTable->get();

        echo "<pre>";
        print_r($query->getRow());
    }

We can use the same methods what we used inside raw queries.

  • Single Row Data (Object Format) – $query->getRow() / $query->getRowObject()
  • Single Row Data in Array Format – $query->getRowArray()
  • First Single Row – $query->getFirstRow()
  • Last Single Row – $query->getLastRow()

Idea #3 – Working with Model

Suppose you have any controller and used model concept to get single row data.

<?php

namespace App\Controllers;

use App\Models\UserModel;

class Site extends BaseController
{
    public function getUsers()
    {
        $userObject = new UserModel();

        $data = $userObject->first();

        echo "<pre>";
        print_r($data);
    }
}

Single row data in Array format

public function getUsers()
   {
        $userObject = new UserModel();

        $data = $userObject->first();

        echo "<pre>";
        print_r($data);
    }

Get Last row data in Array format

public function getUsers()
    {
        $userObject = new UserModel();

        $data = $userObject->orderBy("id", "desc")->first();

        echo "<pre>";
        print_r($data);
    }

We hope this article helped you to How To Get Single Row Data in CodeIgniter 4 Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here