Call MySQL Stored Procedure in CodeIgniter 4 Tutorial

Reading Time: 4 minutes
12,922 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.

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.


Create Database

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

We will use MySQL command to create database. Run this command into Sql tab of PhpMyAdmin.

CREATE DATABASE codeigniter4_app;

Successfully, we have created a database.


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.


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

Open project terminal and start development server via command:

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.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more