How to connect to MySQL database in PHP 7 2019-12-04 09:17 | Views: 206

There are two current procedures to enable you to connect php7 to your database.

  • MySQLi (object-oriented)
  • PHP Data Objects - PDO (object-oriented)

MySQLi (object-oriented)


<?php
$servername = "localhost";
$username = "username";
$password = "password";


// Create connection
$conn = new mysqli($servername, $username, $password);


// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

MySQLi (object-oriented)


<?php
$servername = "localhost";
$username = "username";
$password = "password";


try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

I recommend that you use PDO


Please refer to https://www.w3schools.com/php/php_mysql_connect.asp for more info.


What are you looking for?


Processing Your Request. Please Wait...