The MySQLi library provides way to connect to a MySQL database. You can use two approaches: procedural and object-oriented.
MySQL PHP Connection. Code example
May 4, 2022
PHP OOP connection:
$conn = new mysqli("localhost", "root", "password");
Let’s check if the connection is made:
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
Now let’s put it all together and connect to the local MySQL server:
<?php $conn = new mysqli("localhost", "root", "password"); if($conn->connect_error){ die("Error: " . $conn->connect_error); } echo "Connected"; $conn->close();//closes the connection ?>