Repository navigation
Expand file tree
/
Copy pathandwherePDO.class.php
More file actions
89 lines (78 loc) · 2.6 KB
/
Copy pathandwherePDO.class.php
File metadata and controls
89 lines (78 loc) · 2.6 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Class to handle an arbitraty number of variables to be inserted into a
* PDO-style mysql query. Allows output of the PDO-style query string with
* :placeholders, and correspondingly handles bindParam requests for those
* :placeholders.
*
* @author Ken Irwin <kirwin@wittenberg.edu>
*/
class AndWherePDO {
/**
* Array of condition arrays
* @var array
* @access private
*/
private $conditions; //array of condition-sets
/**
* constructor creates an empty array
* @access public
*/
public function __construct () {
$this->conditions = array();
}
/**
* adds a new array describing one MySQL "AND" condition
* @param string $field Name of MySQL field
* @param string $value The search value to be matched in $field
* @param string $operator MySQL operator such as 'LIKE', '=', '!='
describing the relationship between field and value
* @param string $type Type of value: int, str, bool, null
* @access public
*/
public function AddCondition ($field,$value,$operator='=',$type='str') {
$placeholder = ':' . $field;
switch ($type) {
case 'str':
$param_type = PDO::PARAM_STR;
break;
case 'int':
$param_type = PDO::PARAM_INT;
break;
case 'bool':
$param_type = PDO::PARAM_BOOL;
break;
case 'null':
$param_type = PDO::PARAM_NULL;
break;
}
$condition = array ('field' => $field,
'value' => $value,
'operator' => $operator,
'type' => $type,
'param_type' => $param_type,
'placeholder' => $placeholder,
);
array_push($this->conditions, $condition);
} //end AddCondition
/**
* Takes $conditions array and generates an SQL query string to be
* appended to a WHERE query. Each condition begins with 'AND'
* e.g. 'AND date = :date AND deleted = :deleted
* @access public
*/
public function AndWhereString () {
$return_string = "";
foreach ($this->conditions as $a) {
$return_string .= ' AND '.$a['field'].' '.$a['operator'].' '.$a['placeholder'];
}
return $return_string;
}
public function Bind ($stmt) {
foreach ($this->conditions as $a) {
$stmt->bindParam($a['placeholder'], $a['value'], $a['param_type']);
}
return $stmt;
}
} //end class
?>