pdo bind value code example

Example 1: bind in pdo

$sql = "SELECT * FROM myTable WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->execute([$id]);

Example 2: bind method in pdo in php

If we had a value:

$sql = "SELECT * FROM myTable WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1,1);
$stmt->execute();

If we had two values: 

$sql = "SELECT * FROM myTable WHERE id = ? and name = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1,1);
$stmt->bindValue(2,"alireza");
$stmt->execute();

Example 3: bind in pdo

$sql = "SELECT * FROM myTable WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->execute([":id"=>$id]);

Tags:

Php Example