1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5$dbname = "myDB";
6
7// Create connection
8$conn= mysqli_connect($servername,$username,$password,$dbname);
9// Check connection
10if (!$conn) {
11 die("Connection failed: " . mysqli_connect_error());
12}
13echo "Connected Successfully.";
14?>
1$servername = "localhost";
2$username = "username";
3$password = "password";
4
5// Create connection
6$conn = new mysqli($servername, $username, $password);
7
8// Check connection
9if ($conn->connect_error) {
10 die("Connection failed: " . $conn->connect_error);
11}
12echo "Connected successfully";
13
14
15Simplified
16
17$conn = mysqli_connect('localhost', 'username', 'password');
18$database = mysqli_select_db($conn, 'database');
1<?php
2
3$hostName = 'localhost';
4$userNmame = 'root';
5$password = '';
6$dbname = 'topproduct';
7$db_name = "mysql:host=$hostName;dbname=$dbname";
8$conn = new PDO($db_name,$userNmame,$password);
9
10if(!$conn){
11 echo 'Error database connection';
12}
13
1<?php
2
3function init_db()
4{
5 try {
6
7 $host = 'localhost';
8 $dbname = 'db_name';
9 $charset = 'utf8';
10 $user = 'root';
11 $password = '';
12
13 $db = new PDO("mysql:host=$host;dbname=$dbname;charset=$charset",
14 $user,
15 $password);
16
17 } catch (Exception $e) {
18 die('Erreur : ' . $e->getMessage());
19 }
20
21 return $db;
22}