markus
markus

Reputation: 485

include php in class and use variable from include

i want to access $mysqli from db_connect.db in my class

db_connect.php

<?php
include_once("db.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
mysqli_set_charset($mysqli,"utf8");
?>

class.php

class class{

    function __construct() {
        include_once 'db_connect.php';
    }

    function get()
    {
        if (!$mysqli->connect_errno) {
    }
    }
}

i can access $mysqli if i include the db_connect.php directly in a php function without a class

edit and i want to access $mysqli in all functions of the class

thanks

Upvotes: 2

Views: 48

Answers (1)

Phillip Bartschinski
Phillip Bartschinski

Reputation: 643

you can save it in a class variable:

class class{
    private $mysqli;

    function __construct() {
        include_once 'db_connect.php';

        $this->mysqli = $mysqli;
    }

    function get()
    {
        if (!$this->mysqli->connect_errno) {
        }
    }
}

Upvotes: 2

Related Questions