Good afternoon. Write a class for connecting to database and further withdrawal information.
<?phpclass DB{protected $connection;public function __construct($host, $user, $password, $db_name) {$this->connection = new mysqli($host, $user, $password, $db_name); if( !$this->connection ) { throw new Exception('error'); } } public function getAllInformationFromTableUsers() { $getAllInformationFromTableUsers = $this->connection->query("SELECT * FROM tableUs"); $row = $getAllInformationFromTableUsers->fetch_assoc(); $this->connection->close(); return $row["id"]; } }
Tell me where and how to declare variables
$host, $user, $password, $db_name. The fact is that when you declare them as follows:
<?phpclass DB{public $host = "localhost";public $user = "example";public $password = "12345678";public $db_name = "exampledb";protected $connection;public function __construct($host, $user, $password, $db_name) {$this->connection = new mysqli($host, $user, $password, $db_name); if( !$this->connection ) { throw new Exception('error'); } } }
I get an error
Missing argument 1 for DB::__construct()Not an expert in OOP, but would like to learn this style of writing code.