xxxxxxxxxx
// connect to PDO
$pdo = new PDO("mysql:host=localhost;dbname=myDatabase", $username, $password);
// throw Exceptions for every error.
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare the statement. the placeholders allow PDO to handle substituting
// Security -> prevents SQL injection
$stmt = $pdo->prepare("SELECT order_numbers FROM myTable WHERE customer_id =:cId");
// bind the parameters
$stmt->bindValue(":order_numbers", $orderNumger);
// initialise an array for the results
$products = array();
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$orders[] = $row;
}
print_r($orders);
xxxxxxxxxx
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDatabase";
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$id='1';
$stmt = $pdo->prepare("SELECT * FROM myTable WHERE id =?");
$stmt->execute([$id]);
$results = $stmt->fetch();
//Close DB connection
$pdo = null;
//return results in an array
print_r($results);