S
S
seredaes2015-11-28 01:04:10
PHP
seredaes, 2015-11-28 01:04:10

How to get access to mysqli?

There is such code
function db_init()
{
$mysql_url = "localhost";
$mysql_login = "root";
$mysql_password = "";
$mysql_dbname = "test";
global $sqlid;
$sqlid = new mysqli($mysql_url, $mysql_login, $mysql_password, $mysql_dbname);
}
// Start configuration
db_init();
function test(){
// Test
$result = $sqlid->query("SELECT * FROM table_test ORDER BY id DESC");
while ($row = $result->fetch_assoc()) {
echo $row["content"] . "<br>";
}
$result->close();
}
test($sqlid);
Says he doesn't know $sqlid.
If I pass this $sqlid to the function parameter - test($sqlid);
then everything works...
Is there any way to avoid this? How to make global visibility?
Regarding the db_init function - I can have several configurations,
I would like to quickly switch between them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2015-11-28
@seredaes

1. Decorate in a class and make a variable inside it.
2. Create a variable OUTSIDE functions and use global $sqlid inside functions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question