Connect Server
try {
$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
} catch(PDOException $ex) {
echo "Error occured during connecting to the server";
}
- dsn: Data Source Name
- pdo: PHP Data Object
alternative name of pdo
- dbh: DataBase Handling
- sth: Statement Handling
Fetch All
bindParam
$stmt = $pdo->prepare("SELECT password FROM users WHERE id = :username");
$stmt->bindParam(':username', $id);
$stmt->execute();
if ($stmt->rowCount() == 0) { // When Nothing is found
header('Location: ./index.php');
exit();
}