How To Get Single Row Data in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
9,216 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 –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Database Connection

Open .env file from project root.

Search for DATABASE. You should see the connection environment variables into it. Put your updated details of database connection string.

 
#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = codeigniter4_app
database.default.username = root
database.default.password = root
database.default.DBDriver = MySQLi
database.default.DBPrefix =
   

Now, database successfully connected with the 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

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5