1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace JAQB\Operations; |
12
|
|
|
|
13
|
|
|
use PDO; |
14
|
|
|
|
15
|
|
|
trait Fetchable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Executes a query and returns the first row. |
19
|
|
|
* |
20
|
|
|
* @param int $style PDO fetch style |
21
|
|
|
* |
22
|
|
|
* @return mixed|bool result |
23
|
|
|
*/ |
24
|
|
|
public function one($style = PDO::FETCH_ASSOC) |
25
|
|
|
{ |
26
|
|
|
$stmt = $this->execute(); |
27
|
|
|
|
28
|
|
|
if ($stmt) { |
29
|
|
|
return $stmt->fetch($style); |
30
|
|
|
} else { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Executes a query and returns all of the rows. |
37
|
|
|
* |
38
|
|
|
* @param int $style PDO fetch style |
39
|
|
|
* |
40
|
|
|
* @return mixed|bool result |
41
|
|
|
*/ |
42
|
|
|
public function all($style = PDO::FETCH_ASSOC) |
43
|
|
|
{ |
44
|
|
|
$stmt = $this->execute(); |
45
|
|
|
|
46
|
|
|
if ($stmt) { |
47
|
|
|
return $stmt->fetchAll($style); |
48
|
|
|
} else { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Executes a query and returns a column from all rows. |
55
|
|
|
* |
56
|
|
|
* @param int $index zero-indexed column to fetch |
57
|
|
|
* |
58
|
|
|
* @return mixed|bool result |
59
|
|
|
*/ |
60
|
|
|
public function column($index = 0) |
61
|
|
|
{ |
62
|
|
|
$stmt = $this->execute(); |
63
|
|
|
|
64
|
|
|
if ($stmt) { |
65
|
|
|
return $stmt->fetchAll(PDO::FETCH_COLUMN, $index); |
66
|
|
|
} else { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Executes a query and returns a value from the first row. |
73
|
|
|
* |
74
|
|
|
* @param int $index zero-indexed column to fetch |
75
|
|
|
* |
76
|
|
|
* @return mixed|bool result |
77
|
|
|
*/ |
78
|
|
|
public function scalar($index = 0) |
79
|
|
|
{ |
80
|
|
|
$stmt = $this->execute(); |
81
|
|
|
|
82
|
|
|
if ($stmt) { |
83
|
|
|
return $stmt->fetchColumn($index); |
84
|
|
|
} else { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|