PHP REST API CRUD – Step 3

PHP REST API CRUD – Step 3

Step 3 Create a directory for configuration

  • Create the folder in XAMP/P -> htdocs with any name like PHP REST API CRUD with JWT Authentication
  • Open this folder and create api folder and necessary files if required like index, style, script etc.
  • Open api folder and create config folder.
  • Open config folder and create a new file called database.php

<?php

class Database

{

    //Specify Database Credential

    private $host = “localhost”;

    private $dbName = “phprestapi”;

    private $userName = “root”;

    private $password = “”;

    public $conn;

    //get the Database Connection

    public function getConnection()

    {

        $this->conn = null;

        try {

            $this->conn = new PDO(“mysql:host=” . $this->host . “;dbname=” .

                $this->dbName, $this->userName, $this->password);

            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        } catch (PDOException $exception) {

            echo “Connection error: ” . $exception->getMessage();

        }

        return $this->conn;

    }

}

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*