MySQL with PHP Sandbox

CSC-329 · Semester V · Web Technology

MySQL with PHP

Prepare, execute, fetch — safely, with prepared statements.

SELECT * FROM books WHERE available_copies > 0;

Result

col 1col 2col 3col 4
bk001Data StructuresAho3
bk002Database SystemsElmasri2
bk004Computer NetworksTanenbaum5

$pdo = new PDO("mysql:host=localhost;dbname=library", $user, $pass);

$stmt = $pdo->prepare(<<<SQL

SELECT * FROM books WHERE available_copies > 0;

SQL);

$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

echo htmlspecialchars($row['title'] ?? '');

}

Prepared statements prevent SQL injection — values are never interpolated into the query text.