2

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

0

1 Answer 1

2

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) {
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.