0

My database name is not constant it depends on who logged in so i used session to get the database name but the problem now is how do i reference it inside the class. My code here

<?php

$dbn= $_SESSION['dbname']; //database name

class Invoice{
    private $host  = 'localhost';
    private $user  = 'root';
    private $password   = "";
    private $database  = $dbn;  //database name display here
    private $invoiceUserTable = 'users';    
    private $invoiceOrderTable = 'orders';
    private $invoiceOrderItemTable = 'order_item';
    private $dbConnect = false;
    public function __construct(){
        if(!$this->dbConnect){ 
            $conn = new mysqli($this->host, $this->user, $this->password, $this->database);
            if($conn->connect_error){
                die("Error failed to connect to MySQL: " . $conn->connect_error);
            }else{
                $this->dbConnect = $conn;
            }
        }
    }

Am trying to make $dbn= 'dbname' labelled/comment 'database name' to display where I have commented 'database name display here'

Tried to use private $database = $_SESSION['dbname']; but with no luck.

Thank you in advance

sally
  • 29
  • 7
  • `$dbn` is not accessible inside of the close due to scope. You cannot use `private $database = $_SESSION['dbname'];` as a class property definition because all definitions must use simple values (string, int, etc). You can create the property without a default value, then set it inside of your `__construct()` function. – aynber Aug 23 '22 at 16:09
  • PLease help this is my first time dealing with classes, so i will really appreciate help – sally Aug 23 '22 at 16:37

0 Answers0