forked from itomski/phpstarter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbobjects.php
58 lines (43 loc) · 1.31 KB
/
dbobjects.php
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
57
58
<?php
require_once 'includes/autoload.php';
const DEBUG = true;
require_once 'includes/conf.php';
// DSN = Data Source Name
$dsn = "mysql:host=localhost;port=8889;dbname=phpstarter";
$options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => true, // Verwendet eine offene Verbindung wieder
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
];
try {
// Verbindung zu DB aufbauen
$dbh = new PDO($dsn, 'root', 'root', $options);
$mapper = new UserMapper($dbh);
$results = $mapper->findAll();
$peter = $mapper->findById(1);
$mapper->delete($peter);
/*
$sql = 'SELECT * FROM users';
$stmt = $dbh->query($sql); // Anfrage abschicken
// Ändert die Abfrage von Array auf User-Objects
$stmt->setFetchMode(PDO::FETCH_CLASS, 'User');
$results = $stmt->fetchAll(); // Ergebnisse abholen
*/
//debug($results);
/*
foreach($results as $user) {
echo $user."\n"; // Verwendet automatisch die __toString-Methode
}
*/
$user = new User();
$user->setFirstname('Natasha');
$user->setLastname('Romanov');
$user->setLocation('Berlin');
//saveUser($dbh, $user);
//$mapper->save($user);
}
catch(PDOException $e) {
//print_r($e);
echo $e->getMessage();
}