-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathprepared-statement-results.php
More file actions
56 lines (50 loc) · 1.53 KB
/
prepared-statement-results.php
File metadata and controls
56 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
require 'settings.php';
// PDO connection
try {
$pdo = new PDO(
sprintf(
'mysql:host=%s;dbname=%s;port=%s;charset=%s',
$settings['host'],
$settings['name'],
$settings['port'],
$settings['charset']
),
$settings['username'],
$settings['password']
);
} catch (PDOException $e) {
// Database connection failed
echo "Database connection failed";
exit;
}
// Build and execute SQL query
$sql = 'SELECT id, email FROM users WHERE email = :email';
$statement = $pdo->prepare($sql);
$email = filter_input(INPUT_GET, 'email');
$statement->bindValue(':email', $email, PDO::PARAM_STR);
// Iterate results one at a time
echo 'One result as a time as associative array', PHP_EOL;
$statement->execute();
while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
echo $result['email'], PHP_EOL;
}
// Iterate ALL results at once
echo 'All results at once as associative array', PHP_EOL;
$statement->execute();
$allResults = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($allResults as $result) {
echo $result['email'], PHP_EOL;
}
// Fetch one column value at a time
echo 'Fetch one column, one row at a time as associative array', PHP_EOL;
$statement->execute();
while (($email = $statement->fetchColumn(1)) !== false) {
echo $email, PHP_EOL;
}
// Iterate results as objects
echo 'One result as a time as object', PHP_EOL;
$statement->execute();
while (($result = $statement->fetchObject()) !== false) {
echo $result->email, PHP_EOL;
}