-
Notifications
You must be signed in to change notification settings - Fork 60
/
example.php
213 lines (182 loc) · 4.58 KB
/
example.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* example.php
* Displays some examples of class.db.php usage
*
* @author Bennett Stone
* @version 1.0
* @date 18-Feb-2013
* @package class.db.php
**/
define( 'DB_HOST', 'localhost' ); // set database host
define( 'DB_USER', 'root' ); // set database user
define( 'DB_PASS', 'root' ); // set database password
define( 'DB_NAME', 'yourdatabasename' ); // set database name
define( 'SEND_ERRORS_TO', 'you@yourwebsite.com' ); //set email notification email address
define( 'DISPLAY_DEBUG', true ); //display db errors?
require_once( 'class.db.php' );
//Initiate the class
$database = new DB();
//OR...
$database = DB::getInstance();
/**
* Filter all post data
*/
$_POST['name'] = 'This database class is "super awesome" & whatnots';
if( isset( $_POST ) )
{
foreach( $_POST as $key => $value )
{
$_POST[$key] = $database->filter( $value );
}
}
echo '<pre>';
print_r($_POST);
echo '</pre>';
/**
* Auto filter an entire array
*/
$array = array(
'name' => array( 'first' => '"Super awesome"' ),
'email' => '%&&<stuff',
'something_else' => "'single quotes are awesome'"
);
$array = $database->filter( $array );
echo '<pre>';
print_r($array);
echo '</pre>';
/**
* Retrieve results of a standard query
*/
$query = "SELECT group_name FROM example_phpmvc";
$results = $database->get_results( $query );
foreach( $results as $row )
{
echo $row['group_name'] .'<br />';
}
/**
* Retrieving a single row of data
*/
$query = "SELECT group_id, group_name, group_parent FROM example_phpmvc WHERE group_name LIKE '%Awesome%'";
if( $database->num_rows( $query ) > 0 )
{
list( $id, $name, $parent ) = $database->get_row( $query );
echo "<p>With an ID of $id, $name has a parent of $parent</p>";
}
else
{
echo 'No results found for a group name like "production"';
}
/**
* Inserting data
*/
//The fields and values to insert
$names = array(
'group_parent' => 18,
'group_name' => mt_rand(0, 500) //Random thing to insert
);
$add_query = $database->insert( 'example_phpmvc', $names );
if( $add_query )
{
echo '<p>Successfully inserted "'. $names['group_name']. '" into the database.</p>';
}
$last = $database->lastid();
/**
* Insert multiple records in single query
*/
//Field names
$fields = array(
'group_parent',
'group_name'
);
//Values to insert
$records = array(
array(
9, 'Record 9'
),
array(
7, 'Record 7'
),
array(
7, 'Nick', 'nick@nick.com', 1, 'This will not be added'
),
array(
2, 'This is awesome'
)
);
$inserted = $database->insert_multi( 'example_phpmvc', $fields, $records );
if( $inserted )
{
echo '<p>'.$inserted .' records inserted</p>';
}
/**
* Updating data
*/
//Fields and values to update
$update = array(
'group_name' => md5( mt_rand(0, 500) ),
'group_parent' => 91
);
//Add the WHERE clauses
$where_clause = array(
'group_id' => $last
);
$updated = $database->update( 'example_phpmvc', $update, $where_clause, 1 );
if( $updated )
{
echo '<p>Successfully updated '.$where_clause['group_id']. ' to '. $update['group_name'].'</p>';
}
/**
* Deleting data
*/
//Run a query to delete rows from table where id = 3 and name = Awesome, LIMIT 1
$delete = array(
'group_id' => 15,
'group_name' => 'Production Tools (updated)'
);
$deleted = $database->delete( 'example_phpmvc', $delete, 1 );
if( $deleted )
{
echo '<p>Successfully deleted '.$delete['group_name'] .' from the database.</p>';
}
/**
* Checking to see if a value exists
*/
$check_column = 'group_id';
$check_for = array( 'group_name' => 'Resources' );
$exists = $database->exists( 'example_phpmvc', $check_column, $check_for );
if( $exists )
{
echo '<p>Bennett DOES exist!</p>';
}
/**
* Checking to see if a table exists
*/
if( !$database->table_exists( 'example_phpmvc' ) )
{
//Run a table install, the table doesn't exist
}
/**
* Truncating tables
* Commented out intentionally (just in case!)
*/
//Truncate a single table, no output display
//$truncate = $database->truncate( array('example_phpmvc') );
//Truncate multiple tables, display number of tables truncated
//echo $database->truncate( array('example_phpmvc', 'my_other_table') );
/**
* List the fields in a table
*/
$fields = $database->list_fields( "SELECT * FROM example_phpmvc" );
echo '<pre>';
print_r( $fields );
echo '</pre>';
/**
* Output the number of fields in a table
*/
echo $database->num_fields( "SELECT * FROM example_phpmvc" );
/**
* Display the number of queries performed by the class
* Applies across multiple instances of the DB class
*/
echo '<hr />' . $database->total_queries();