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<?php
2$db = "mysql:host=localhost;dbname=pdolearn";
3$user = "root";
4$password = "";
5
6$pdo = new PDO($db, $user, $password);
7
8// $stmt = "SELECT * FROM countries"; //for mySQL
9$stmt = $pdo->query("SELECT * from countries");
10
11// while($row = mysqli_fetch_assoc($stmt)) //for mysql
12while ($row = $stmt->fetch())
13{
14 echo "<pre>";
15
16 print_r($row);
17}
18
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}
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} catch(PDOException $e) {
12 echo "Connection failed: " . $e->getMessage();
13}
14?>
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 }