parameterized_query.php

PHOTO EMBED

Thu Sep 08 2022 09:30:39 GMT+0000 (UTC)

Saved by @vmap23 #php #mysql #pdo

<?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;

?>
content_copyCOPY

https://zetcode.com/php/pdo/