How to fix the error when database connection from a function?

In the file api.php I wrote 2 functions.
The first connects with the database. The second calls a function on the database connection and selects data from the database.
In the file index.php I call the second function. And get the error.
Fatal error: Class 'my_api\mysqli' not found in Z:\domains\test_site\api.php on line 102
Line 102
$mysqli = new mysqli($hostname, $username, $password, $base_name);

How to fix the error when database connection from a function?

File api.php
namespace my_api;
//Database connection
connect_to_base function($base_name)
{
$hostname='localhost';
$username='root';
$password='Ser45Ddf';
if ($_SERVER["REMOTE_ADDR"]=='127.0.0.1') $password="; //Password for the local host
$mysqli = new mysqli($hostname, $username, $password, $base_name); // <b>line 102</b>
if (mysqli_connect_errno()) {printf("Connect failed: %s\n", mysqli_connect_error());exit();}
return $mysqli;
}

//Function call for the database connection
function my_func()
{
$mysqli=connect_to_base('statistics'); //gets the object?
$mysqli_result=$mysqli->query("SELECT * FROM `day_data` ORDER BY id DESC LIMIT 1");
//...............
}


File index.php
include_once (__DIR__.'/api.php'); //Checked - file is connected correctly and other functions work

$a=my_api\my_func(); //Fatal error: Class 'my_api\mysqli' not found in Z:\domains\test_site\api.php on line 102
echo $a;
June 3rd 19 at 19:08
1 answer
June 3rd 19 at 19:10
Solution
the namespace set for the global class mysqli so, maybe you set your custom, and there is no such:
new \mysqli

And why the OOP style of connection to use in procedural code?
the namespace set to mysqli so, maybe you set your custom, and there is no such class

Sorry, I do not understand. What's the class? I have no classes.

For example
File api.php
namespace my_api;
function geta()
{
$a=1; 
return $a;
}


File index.php
include_once (__DIR__.'/api.php');
$a=my_api\geta();

Will work.
I'm all set. It should return a result object. - johnnie55 commented on June 3rd 19 at 19:13
but you have to create an object, using new mysqli(//connection data)
But you have a custom namespace, and we need to fix it!

Here you have this happening:
if ($_SERVER["REMOTE_ADDR"]=='127.0.0.1') $password="; //Password for the local host
$mysqli = new mysqli($hostname, $username, $password, $base_name);


next, just add the slash back - Mohamed_Lind75 commented on June 3rd 19 at 19:16
next, just add the slash back

Seems to work.

But you have a custom namespace, and we need to fix it!

But everything in the file with one namespace must belong to this namespace.
new mysqli
In this file I do not need to specify the/think about the namespaces.
I created an instance of an object, function, array, and they must belong to this namespace.

Or do you want to say that the object instance new mysqli extension does not belong to the namespace my_api? - johnnie55 commented on June 3rd 19 at 19:19
of course not! this class global
php.net/manual/ru/language.namespaces.global.php - Mohamed_Lind75 commented on June 3rd 19 at 19:22
Happened, but I decided to abandon this idea.
The namespace and the implementation scope objects still disappointing. - johnnie55 commented on June 3rd 19 at 19:25

Find more questions by tags PHP