php mysqli conneection object oriented

Today we are show you how to create database connection with object oriented PHP features with mysqli type connection using php class and function.
Here we are also going to display all the data of a table this table available in my local system database. We had done this before using a procedural way, but in this tutorial we are going to try it using object oriented PHP features.

Step 1: First of a you should create a php file “connect.php” and copy past below code inside connect.php file and save. Then create class “createCon” define all required variables, create a construct, inside construct create a connection.

<?php
class createCon  {
    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'test';
    var $myconn;

    function __construct() {
        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if (!$con) {
            die('Could not connect to database!');
        } else {
            $this->myconn = $con;
            //echo 'Connection established!';
        }
        return $this->myconn;
    }

    function close() {
        mysqli_close($myconn);
        echo 'Connection closed!';
    }
}

class fetchData{
    private $obj;
    function __construct(){
        $this->obj = new createCon();
    }
    function fetchTestData()
    {
        //$obj = new createCon();
        $query = 'SELECT * FROM  `tempdata`';
        $result = mysqli_query($this->obj->myconn, $query);
        if($numrows = mysqli_num_rows($result))
        {
            while ($row = mysqli_fetch_assoc($result))
            {
                echo "<br>".$row['name'];
                echo "<br>".$row['email'];
            }
        }
    }
}
?>

Step 2: Just create one more file with php extension such as “index.php” inside index.php file include the connect.php file and create object of fetch-data class and call the function using class object which use to show test data from a table.

<?php
include 'connect.php';

$Obj = new fetchData();
$Obj->fetchTestData();
?>

Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.