mysql - global variable not working in php class -


class database {     public $connect = "";      public function connect()     {        $connect = new mysqli("localhost", "root", "1521", "phone");        ini_set('default_charset', "utf-8");        mysqli_set_charset($connect, "utf8");        header('content-type: text/html; charset=utf-8');        if ($connect->connect_error) {            die('not connect' . mysqli_connect_error());        }     }      public function insert()     {         $sql = "select id personal order id desc limit 1";         //global $connect;         $result = $connect->query($sql);//// here !!!!!!!!         $row = $result->fetch_assoc();         $tempint = (int)$row['id'];         $tempint += 1;     } } 

why $connect in insert function not working ? added global befor $connect still not working. simple class mysql.

you syntax incorrect.

change this,

$connect = new mysqli("localhost", "root", "1521", "phone"); 

to,

$this->connect = new mysqli("localhost", "root", "1521", "phone"); 

also,

$result = $connect->query($sql); 

to,

$result = $this->connect->query($sql); 

with above changes should fix issue.

$connect has been created in scope of class when refer need use $this.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -