0

I wanted to start my a new project but this time using mysqli object, here is what I have for the database class:

<?php

declare(strict_types = 1);
namespace A\B\App;

class Database
{

    private $server = "abc";
    private $user   = "abc";
    private $pass   = "abcabc";
    private $name   = "localhost";

    public function __construct() 
    {
        $this->connect();
    }

    public function connect() 
    {

        $mysqli = new mysqli($server, $user, $pass, $name);
        if($mysqli->connect_errno)
        {
            echo "failed to connect";
            exit();
        }

    }
}

and here on my index page im simply calling the database class just to see if the connection is all good:

<?php 

use A\B\App\Database;

require_once("../../vendor/autoload.php");

$db = new Database();

but this throws a fatal error:

Fatal error: Uncaught Error: Class "A\B\App\mysqli" not found in /Applications/MAMP/htdocs/ab/src/App/Database.php:24 Stack trace: #0 /Applications/MAMP/htdocs/ab/src/App/Database.php(18): A\B\App\Database->connect() #1 /Applications/MAMP/htdocs/ab/src/Pages/index.php(10): A\B\App\Database->__construct() #2 {main} thrown in /Applications/MAMP/htdocs/ab/src/App/Database.php on line 24

Please note that the composer autoload is working correctly since I tried to simply echo text from the __construct method in the database and it showed perfectly fine.

Blu3
  • 143
  • 2
  • 19
  • 1
    `$mysqli = new \mysqli($server, $user, $pass, $name);` – Barmar Jul 23 '21 at 20:59
  • you my friend are correct, this did the trick. Thank you very much. It would be nice if you answered this outside the comment so I can mark it solved for others in the future too ;) – Blu3 Jul 23 '21 at 21:26
  • 1
    The answer is in the duplicate question. I put it in the comment as a summary. – Barmar Jul 23 '21 at 21:29
  • well I saw that answer but did not think it was the same issue with me, sometimes we need to see more of an identical issue to understand the answer – Blu3 Jul 23 '21 at 21:35
  • 2
    It's the same issue. Your code is in `namespace A\B\App`. So it's looking for `A\B\App\mysqli`. You want `mysqli` from the root namespace. – Barmar Jul 23 '21 at 21:45

0 Answers0