Call MySQL Stored Procedure in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
3,027 Views

A procedure (often called a stored procedure) is a collection of pre-compiled SQL statements stored inside the database. It is a subroutine or a subprogram in the regular computing language.

Inside this article we will see How to run/call MySQL stored procedure in CodeIgniter 4. This article will be very interesting to know and learn. We will see the complete guide of mysql stored procedure creation and calling from a codeigniter 4 application.

How to use a mysql stored procedure in codeigniter 4, all things will be cleared in this article.

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.

x
Call MySQL Stored Procedure in CodeIgniter 4 Tutorial

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.


Create Database in Application

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE codeigniter4_app;

Let’s connect with the application.


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.


Create MySQL Stored Procedure

Open MySQL database.

Let’s say we have a table in database called as blogs. In this table we have 100 rows. By the help of procedure, we will read all blogs as well as any specific blog.

We will create stored procedure by using phpmyadmin manual interface option to add routines.

We will create two MySQL stored procedure here for this article.

  • Read all blogs from table
  • Read a Single Blog from Table

Read all blogs from table

Read a Single Blog from Table

Successfully, we have created two mysql stored procedures in database.


Create Routes

Open Routes.php from /app/Config folder. Inside this file we will add few routes which calls methods of controller.

Add this code into your Routes.php file,

//...

$routes->get("list-blog", "DataController::listBlogs");
$routes->get("single-blog/(:num)", "DataController::singleBlog/$1");

Create Controller

Open project into terminal and run this spark command to create controller.

$ php spark make:controller Data --suffix

It will create DataController.php file at /app/Controllers folder.

Open DataController.php and write this following code.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class DataController extends BaseController
{
    public function __construct()
	{
		// Loading db instance
		$this->db = db_connect();
	}
    public function listBlogs()
    {
        $blogs = $this->db->query("CALL getBlogs()")->getResult();

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

    public function singleBlog($id)
    {
        $single_blog = $this->db->query("CALL getSingleBlogDetail(" . $id . ")")->getRow();

        echo "<pre>";
        print_r($single_blog);
    }
}
  • Using CALL getBlogs() to call, list all blogs
  • Using CALL getSingleBlogDetail(” . $id . “) to get specific blog detail.

Application Testing

Start development server:

$ php spark serve

URL To List All Blogs – http://localhost:8080/list-blog

URL To Single Blog – http://localhost:8080/single-blog/3

We hope this article helped you to learn about Call MySQL Stored Procedure in CodeIgniter 4 Tutorial in a very detailed way.

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