Setting up a database connection class in PHP

check getAttributesByShopId() function in V2-growcer and correct it everywhere.

You can do it like this way:-

Dbconnection.php:-

<?php
Class DbConnection{
    function getdbconnect(){
        $conn = mysqli_connect("host","username","password","database") or die("Couldn't connect");
        return $conn;
    }
}
?>

Function.php:-

<?php
require_once('Dbconnection.php');
Class WorkingExamples{
    function getUserInfo($id) {
        $Dbobj = new DbConnection(); 
        $query = mysqli_query($Dbobj->getdbconnect(), "SELECT * FROM users WHERE id = '$id'");
        return mysqli_fetch_array($query);
    }
}
$data = new WorkingExamples();
echo "<pre/>";print_r($data->getUserInfo(3));
?>

Note:- this is an example, try it by changing values according to your requirement and get the result.


<?php

class DatabaseConnection
{
    private $host       = "127.0.0.1";
    private $dbname     = "online_english";
    private $dbUsername = "root";
    private $dbPass     = "";
    private $charset    = 'utf8mb4';
    private $dsn;

    public function tryConnect(){
        try{
            $this->dsn = "mysql:host=$this->host;dbname=$this->dbname;charset=$this->charset";
            $DBH = new PDO($this->dsn,$this->dbUsername,$this->dbPass);
            $DBH->exec("set names utf8");
            $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $DBH;
        }
        catch (PDOException $e){
            $e->getMessage();
        }
    }
}
?>