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?>
1$host = '127.0.0.1';
2$db = 'test';
3$user = 'root';
4$pass = '';
5$charset = 'utf8mb4';
6$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
7$options = [
8 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
9 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
10 PDO::ATTR_EMULATE_PREPARES => false,];
11try {
12 $pdo = new PDO($dsn, $user, $pass, $options);
13} catch (\PDOException $e) {
14 throw new \PDOException($e->getMessage(), (int)$e->getCode());
15}
1<?php
2
3$dsn = "mysql:host=localhost;dbname=mydb";
4$user = "user12";
5$passwd = "12user";
6
7$pdo = new PDO($dsn, $user, $passwd);
8
9