0

I have config.php:

$mysqli = new mysqli($hostname, $user, $pass, $bd);

I include this config.php in all my website pages. eg

index.php

include("config.php");
include("functions.php");
...

in functions.php I have functions like this:

function get_name{
 $stmt = $mysqli->prepare("SELECT * FROM users WHERE user=?");
}

my problem is, function get_name cant see $mysqli var. I need to include config.php inside get_name function (and all functions).

is there any other way to get $mysqli variable without need to include config.php in all functions?

2 Answers 2

1

Use global for variables in functions.

function get_name{
    global $mysqli;
     $stmt = $mysqli->prepare("SELECT * FROM users WHERE user=?");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Use class and objects for this. In config page write down the connection variables & by including the config page in a database class, write your functions in an inherited class then pass it in an object based reference to functions.

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.