What are the differences between using new mysqli and mysqli_connect in PHP?
When working with MySQL databases in PHP, the main difference between using `new mysqli` and `mysqli_connect` lies in the way the connection object is created. `new mysqli` creates an object-oriented instance of the mysqli class, allowing for more flexibility and control over the connection and subsequent queries. On the other hand, `mysqli_connect` is a procedural way of establishing a connection, which may be simpler for basic operations but lacks some of the advanced features of the object-oriented approach.
// Using new mysqli
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Using mysqli_connect
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}