1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

OO and non-OO usage of Mysqli

Discussion in 'Programming/Internet' started by Veljko Stefanovic, Sep 16, 2020.

  1. I'm starting to learn the object oriented aspect of PHP. In this regard I made a small "exercise" of sorts "translating" one of my PHP functions into OO mode of PHP. I'm asking if there are there any recommendations you can give me concerning my predicament.

    Example of non-OO PHP:

    <?php
    function pristup($servername, $username, $password, $dbname, $sql){

    $conn=new mysqli($servername, $username, $password, $dbname);

    if($conn->connect_error){

    die("Neuspela konekcija: ".$conn->connect_error);

    }

    $result = $conn->query($sql);
    if ($result == TRUE) {
    //echo "Uspela konekcija";
    } else {
    echo "Neuspešno izvršavanje upita: " . $conn->error;
    }
    return $result;

    $conn->close();

    }

    ?>


    Example of this done in an object oriented manner:

    <?php

    class konekcija{

    private $servername;
    private $username;
    private $password;
    private $dbname;
    private $sql;

    //Setter functions
    public function setVal($par1, $par2, $par3, $par4, $par5){ //Setovanje vrednosti za upit.
    $this->servername = $par1;
    $this->username = $par2;
    $this->password = $par3;
    $this->dbname = $par4;
    $this->sql = $par5;

    }

    //Getter functions
    public function getServername() {
    return $this->servername;
    }

    public function getUsername() {
    return $this->username;
    }

    public function getPassword() {
    return $this->password;
    }

    public function getDBname() {
    return $this->dbname;
    }

    public function getSQL() {
    return $this->sql;
    }

    //Function that executes query.
    public function pristup($server_name, $user_name, $pass_word, $db_name, $sql_query){

    $conn=new mysqli($server_name, $user_name, $pass_word, $db_name);

    if($conn->connect_error){

    die("Neuspela konekcija: ".$conn->connect_error);

    }

    $result = $conn->query($sql_query);

    if ($result == TRUE){

    echo "Uspela konekcija";

    }
    else{

    echo "Neuspešno izvršavanje upita: " . $conn->error;

    }

    return $this->$result;

    $conn->close();

    }

    }


    $kon = new konekcija(); //Creation of an object.

    //Setting values.
    $kon -> setVal("localhost", "root", "", "test", "CREATE TABLE example(
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )");

    //Getting values and inserting them into class method.
    $kon -> pristup($kon->getServername(), $kon->getUsername(), $kon->getPassword(), $kon->getDBname(), $kon->getSQL());

    ?>


    This works, and I'm asking if this code is any good. Is there a better/more established way of doing this?

    Login To add answer/comment
     

Share This Page