Oop - php

                                                 OOP-PHP   

 

1:config.php

    store all the db related info uder this file as constant:

    

<?php
define("DB_HOST","ecom.loc");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","phptest");

?>

2: Database.php

create a class with suitable name  and initialize the connection from the database

with oop mysqli.

and make a function for selecting the data from the database.

<?php
class Database {

//database configuration variables
public $host = DB_HOST;
public $user = DB_USER;
public $pass = DB_PASS;
public $dbname = DB_NAME;

//connection and error variables
public $conn;
public $error;

//constructor function for calling connectDB
public function __construct(){
$this->connectDB();
}
//function for connecting to the database
public function connectDB(){
$this->conn = new Mysqli($this->host,$this->user,$this->pass,$this->dbname);
if(!$this->conn){
$this->error = 'connection failed'.$this->conn->connect_error;
return false;
}
}

//select Data from database

public function select($query){
return $query;
}

}
?>

 Here $conn is the main db connection object.

To include all this in front we have to include all this files into our front end


<?php
include 'inc/header.php';
include 'config.php';
include 'database.php';
?>
<?php
$db = new Database();
$query = "SELECT * FROM `userDetails`";
$read = $db->select($query);
?>


 

 

Comments

Popular posts from this blog

preparation analytics

Higher package

General info - 6350958828e0b