0

I have Connection class and Operations class when call the required method which is used to insert the record to the database , that shows the error.

File Name : Connection.php

<?php
/**
* Connection class to make "connection" with database
*/
class Connection  
{   
    public $host;
    public $user;
    public $password;
    public $databaseName;

    /**
    * Constructor
    */
public  function __construct()
    {   
        $this->host = '';
        $this->user = '';
        $this->password = '';
        $this->databaseName = '';

        $this->connect();
    }
    /**
    * The method that make the connection to the database
    */

Connection method which is used to establish the connection

    public function connect()
    {
        $con = mysqli_connect($this->host, $this->user, $this->password, $this->databaseName);
        if(!$con){
            echo "Connect to the database failed";
        }

        return $con;
    }

}

FIle Name : Operations.php

 require_once"Connection.php";

    class Operations
    {
        //$Connection = new Connection();
        //$con = $Connection->connect();
        function __construct()
        {
            $Connection = new Connection();
            $con = $Connection->connect();
        }

When i call this following function, it's show me the error

        public function add_main_menu($title, $title_ara, $image)
        {
            $sql = "INSERT INTO menu (title, title_ara, image) VALUES ('$title', '$title_ara', '$image')";
            $result = mysqli_query($con, $sql);

            return $result;
        }

        public function add_sub_menu($title, $title_ara, $price, $description, $descreption_ara, $image, $menu_id){

            $sql = "INSERT INTO sub_menu (title, title_ara, price, description, description_ara, image, menu_id) VALUES ('$title', '$title_ara', $price, '$description', '$description_ara', '$image', '$menu_id')";
            $result = mysqli_query($con, $sql);

            return $result;
        }
    }
K.Suthagar
  • 2,226
  • 1
  • 16
  • 28
  • In your last function, you spelled the parameter incorrectly: `$descreption_ara` instead of `$description_ara`. – Tyler Roper Dec 09 '16 at 17:35
  • 2
    Possible duplicate of [Warning: mysqli\_query() expects parameter 1 to be mysqli, null given in](http://stackoverflow.com/questions/18862743/warning-mysqli-query-expects-parameter-1-to-be-mysqli-null-given-in) – Federkun Dec 09 '16 at 17:35
  • also, http://php.net/manual/en/language.variables.scope.php and http://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php – Federkun Dec 09 '16 at 17:36
  • Okay, but I didn't call the last function yet –  Dec 09 '16 at 17:36
  • 1
    As @Federkun has mentioned, your issue is likely related to scope, not the typo. – Tyler Roper Dec 09 '16 at 17:37
  • Variable `$con` in `__construct` is __not available__ elsewhere – u_mulder Dec 09 '16 at 17:39
  • Try to declare $con as private: `private $con;`, then assign $con to the Connection. – Edward Dec 09 '16 at 17:42
  • so, what is the solution ? –  Dec 09 '16 at 17:42
  • The solution is to learn PHP/OOP properly. – u_mulder Dec 09 '16 at 17:45
  • The error is caused by the "connect" method , I used mysqli_connect this was the issue –  Dec 09 '16 at 21:54
  • Are you sure `host`, `username`, `password`, `databaseName` variables are assigned? – Wolverine Dec 09 '16 at 22:13
  • I found the problem , the solution is replace "mysqli_connect" with "new mysqli" in the method "connect" , thank you all :) –  Dec 09 '16 at 22:16
  • You will get another problem even after you solved this problem. Change `mysqli_query($con, $sql);` to `mysqli_query($this->con, $sql);`. – Wolverine Dec 09 '16 at 22:18
  • You are right , thank you :) –  Dec 09 '16 at 22:19

1 Answers1

0

Change the Connection.php as following,

<?php
class Connection  
{   
    public $host;
    public $user;
    public $password;
    public $databaseName;

public  function __construct($host,$user,$password,$database)
    {   
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->databaseName = $database;

        $this->connect();
    }

Now change the Operations.php file as following

 require_once"Connection.php";
    class Operations
    {
        function __construct()
        {
            $Connection = new Connection("host","user","passwrd","database");
            $con = $Connection->connect();
        }
K.Suthagar
  • 2,226
  • 1
  • 16
  • 28