SHOW:
|
|
- or go back to the newest paste.
1 | - | <?php |
1 | + | G<?php |
2 | - | |
2 | + | |
3 | - | abstract class baseDAO{ |
3 | + | abstract class baseDAO{ |
4 | - | private $__connection; |
4 | + | private $__connection; |
5 | - | |
5 | + | |
6 | - | public function __construct(){ |
6 | + | public function __construct(){ |
7 | - | $this->_connectToDB(DB_USER, DB_PASS, DB_HOST, DB_DATABASE); |
7 | + | $this->_connectToDB(DB_USER, DB_PASS, DB_HOST, DB_DATABASE); |
8 | - | } |
8 | + | } |
9 | - | |
9 | + | |
10 | - | private function __connectToDB($user, $pass, $host, $database){ |
10 | + | private function __connectToDB($user, $pass, $host, $database){ |
11 | - | $this->__connection = mysql_connect($host, $user, $pass); |
11 | + | $this->__connection = mysql_connect($host, $user, $pass); |
12 | - | mysql_select_db($database, $this->__connection); |
12 | + | mysql_select_db($database, $this->__connection); |
13 | - | } |
13 | + | } |
14 | - | |
14 | + | |
15 | - | public function fetch($value, $key = NULL){ |
15 | + | public function fetch($value, $key = NULL){ |
16 | - | if (is_null($key)){ |
16 | + | if (is_null($key)){ |
17 | - | $key = $this- > primaryKey; |
17 | + | $key = $this- > primaryKey; |
18 | - | } |
18 | + | } |
19 | - | |
19 | + | |
20 | - | $sql = "select * from {$this->tableName} where {$key}='{$value}'"; |
20 | + | $sql = "select * from {$this->tableName} where {$key}='{$value}'"; |
21 | - | $results = mysql_query($sql, $this- >__connection); |
21 | + | $results = mysql_query($sql, $this- >__connection); |
22 | - | $rows = array(); |
22 | + | $rows = array(); |
23 | - | |
23 | + | |
24 | - | while ($result = mysql_fetch_array($results)) { |
24 | + | while ($result = mysql_fetch_array($results)) { |
25 | - | $rows[] = $result; |
25 | + | $rows[] = $result; |
26 | - | } |
26 | + | } |
27 | - | return $rows; |
27 | + | return $rows; |
28 | - | } |
28 | + | } |
29 | - | |
29 | + | |
30 | - | public function update($keyedArray){ |
30 | + | public function update($keyedArray){ |
31 | - | $sql = "update {$this->tableName} set "; |
31 | + | $sql = "update {$this->tableName} set "; |
32 | - | $updates = array(); |
32 | + | $updates = array(); |
33 | - | |
33 | + | |
34 | - | foreach ($keyedArray as $column=> $value) { |
34 | + | foreach ($keyedArray as $column=> $value) { |
35 | - | $updates[] = "{$column}='{$value}'"; |
35 | + | $updates[] = "{$column}='{$value}'"; |
36 | - | } |
36 | + | } |
37 | - | |
37 | + | |
38 | - | $sql .= implode(',', $updates); |
38 | + | $sql .= implode(',', $updates); |
39 | - | $sql .= "where {$this->primaryKey}='{$keyedArray[$this->primaryKey]}'"; |
39 | + | $sql .= "where {$this->primaryKey}='{$keyedArray[$this->primaryKey]}'"; |
40 | - | mysql_query($sql, $this->__connection); |
40 | + | mysql_query($sql, $this->__connection); |
41 | - | } |
41 | + | } |
42 | - | } |
42 | + | } |
43 | - | |
43 | + | |
44 | - | class userDAO extends baseDAO{ |
44 | + | class userDAO extends baseDAO{ |
45 | - | protected $_tableName = 'userTable'; |
45 | + | protected $_tableName = 'userTable'; |
46 | - | protected $_primaryKey = 'id'; |
46 | + | protected $_primaryKey = 'id'; |
47 | - | |
47 | + | |
48 | - | public function getUserByFirstName($name){ |
48 | + | public function getUserByFirstName($name){ |
49 | - | $result = $this->fetch($name, 'firstName'); |
49 | + | $result = $this->fetch($name, 'firstName'); |
50 | - | return $result; |
50 | + | return $result; |
51 | - | } |
51 | + | } |
52 | - | } |
52 | + | } |
53 | - | |
53 | + | |
54 | - | |
54 | + | |
55 | - | define('DB_USER', 'user'); |
55 | + | define('DB_USER', 'user'); |
56 | - | define('DB_PASS', 'pass'); |
56 | + | define('DB_PASS', 'pass'); |
57 | - | define('DB_HOST', 'localhost'); |
57 | + | define('DB_HOST', 'localhost'); |
58 | - | define('DB_DATABASE', 'test'); |
58 | + | define('DB_DATABASE', 'test'); |
59 | - | |
59 | + | |
60 | - | $user = new userDAO(); |
60 | + | $user = new userDAO(); |
61 | - | $userDetailsArray = $user->fetch(1); |
61 | + | $userDetailsArray = $user->fetch(1); |
62 | - | $updates = array('id' => 1, 'firstName' => 'aaron'); |
62 | + | $updates = array('id' => 1, 'firstName' => 'aaron'); |
63 | - | $user->update($updates); |
63 | + | $user->update($updates); |
64 | - | $allAarons = $user->getUserByFirstName('aaron'); |
64 | + | $allAarons = $user->getUserByFirstName('aaron'); |
65 | ?> |