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<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5
6try {
7 $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
8 // set the PDO error mode to exception
9 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10 echo "Connected successfully";
11 }
12catch(PDOException $e)
13 {
14 echo "Connection failed: " . $e->getMessage();
15 }
16?>
1$host = "localhost";//Ip of database, in this case my host machine
2$user = "root"; //Username to use
3$pass = "qwerty";//Password for that user
4$dbname = "DB";//Name of the database
5
6try {
7 $connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
8 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
9
10}catch(PDOException $e)
11{
12 echo $e->getMessage();
13}
1public static function connexionBDD()
2 {
3 $base = null;
4
5 try {
6 $base = new PDO('mysql:host=' . MYSQL_HOSTNAME . '; dbname=' . MYSQL_DATABASE . '', MYSQL_USERNAME, MYSQL_PASSWORD);
7 } catch (exception $e) {
8 die('Erreur ' . $e->getMessage());
9 }
10
11 return $base;
12 }
1<?php
2$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
3?>
4
1<?php
2
3 $pdo = new PDO('mysql:host=localhost;
4 dbname=the_name_of_your_databe,
5 'username',
6 'password'');
7# by default your username is root
8# if you don't have a password don't fill in it
9
10#(optional) :
11 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
12 $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
13
14?>