Lesson 8 of 8
MySQL Database Basics
PHP connects to MySQL databases using PDO or MySQLi. PDO is recommended because it supports prepared statements, which protect against SQL injection.
Always use prepared statements when inserting user data into queries.
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();
?>