To connect procedurally, the mysqli_connect function is used, which takes the server address, username and password:
Lets connect to DB:
$conn = mysqli_connect("localhost", "root", "password");
Let’s check if we have connected to the Database:
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
mysqli_connect_error() – returns error information
mysqli_close() – function to close the connection
Full Code for connection
<?php $conn = mysqli_connect("localhost", "root", "mypassword"); if ($conn === false) { die("Error" . mysqli_connect_error()); } echo "Connected"; mysqli_close($conn); ?>
MySQL OOP PHP Connection. Code example
May 4, 2022