CodeIgniter 4 How To Get Single Row Data Tutorial

Reading Time: 5 minutes
12,851 Views

In web development, retrieving a single row of data from a database is a common requirement for various application scenarios. In CodeIgniter 4, fetching a single record efficiently enables developers to access specific data entries, allowing targeted data manipulation and utilization.

In this tutorial, we’ll see the comprehensive process of fetching a single row of data from a database using CodeIgniter 4. This functionality empowers developers to retrieve specific records based on criteria.

There are several methods available to get single row data like using Models, using Query Builder, using Raw Queries.

Read More: CodeIgniter 4 How To Read Parse CSV File Tutorial

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 values.

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

database.default.hostname = localhost
database.default.database = codeigniter4_app
database.default.username = admin
database.default.password = admin
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306
  

Now, database successfully connected with the application.

Read More: CodeIgniter 4 How To Integrate Ckeditor Example Tutorial

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());
}

Read More: How to Use CodeIgniter Components Example Tutorial

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);
}

Read More: CodeIgniter 4 Autocomplete Places Search Using Google Maps JavaScript API

Get Last row data in Array format

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

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

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

That’s it.

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

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.