<?php //In the example, we use bindValue to create a parameterized query. We use question mark placeholder. $dsn = "mysql:host=localhost;dbname=mydb"; $user = "root"; $passwd = "andrea"; $pdo = new PDO($dsn, $user, $passwd); $id = 12; //Say this input comes from a user. $stm = $pdo->prepare("SELECT * FROM countries WHERE id = ?"); $stm->bindValue(1, $id); $stm->execute(); //The select statement fetches a specific row from the table. We bind the value with bindValue to a question mark placeholder. $row = $stm->fetch(PDO::FETCH_ASSOC); echo "Id: " . $row['id'] . PHP_EOL; echo "Name: " . $row['name'] . PHP_EOL; echo "Population: " . $row['population'] . PHP_EOL; ?>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter