Lektion 8 af 8
MySQL database basics
PHP kan forbinde til MySQL med PDO eller MySQLi. PDO anbefales ofte, fordi det understøtter prepared statements, som beskytter mod SQL injection.
PHP
<?php
// Connect with PDO
$pdo = new PDO("mysql:host=localhost;dbname=mydb", "user", "pass");
// Prepared statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$userId]);
$users = $stmt->fetchAll();
?>