-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdal.php
More file actions
45 lines (40 loc) · 1.03 KB
/
dal.php
File metadata and controls
45 lines (40 loc) · 1.03 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
<?php
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
$stmt = $pdo->query('SELECT name FROM users');
while ($row = $stmt->fetch())
{
echo $row['name'] . "\n";
}
// Prevent SQL Injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email AND status=:status');
$stmt->execute(['email' => $email, 'status' => $status]);
while ($row = $stmt->fetch())
{
echo $row['name'] . "\n";
}
//select with foreach
$stmt = $pdo->query('SELECT name FROM users');
foreach ($stmt as $row)
{
echo $row['name'] . "\n";
}
// Insert
$statement = $pdo->prepare("INSERT INTO testtable(name, lastname, age)
VALUES(:fname, :sname, :age)");
$statement->execute(array(
"fname" => "Bob",
"sname" => "Desaunois",
"age" => "18"
));
?>