@@ -15,61 +15,85 @@ When you're ready to get started, see the [Quick Start Guide](http://www.meekro.
15
15
### Manual Setup
16
16
Include the ` db.class.php ` file into your project and set it up like this:
17
17
18
- require_once 'db.class.php';
19
- DB::$user = 'my_database_user';
20
- DB::$password = 'my_database_password';
21
- DB::$dbName = 'my_database_name';
18
+ ``` php
19
+ require_once 'db.class.php';
20
+ DB::$user = 'my_database_user';
21
+ DB::$password = 'my_database_password';
22
+ DB::$dbName = 'my_database_name';
23
+ ```
22
24
23
25
### Composer
24
26
Add this to your ` composer.json `
25
27
26
- {
27
- "require": {
28
- "sergeytsalkov/meekrodb": "*"
29
- }
30
- }
28
+ ``` json
29
+ {
30
+ "require" : {
31
+ "sergeytsalkov/meekrodb" : " *"
32
+ }
33
+ }
34
+ ```
31
35
32
36
Code Examples
33
37
========
34
38
### Grab some rows from the database and print out a field from each row.
35
39
36
- $accounts = DB::query("SELECT * FROM accounts WHERE type = %s AND age > %i", $type, 15);
37
- foreach ($accounts as $account) {
38
- echo $account['username'] . "\n";
39
- }
40
+ ``` php
41
+ $accounts = DB::query("SELECT * FROM accounts WHERE type = %s AND age > %i", $type, 15);
42
+ foreach ($accounts as $account) {
43
+ echo $account['username'] . "\n";
44
+ }
45
+ ```
46
+
47
+
40
48
41
49
### Insert a new row.
42
50
43
- DB::insert('mytable', array(
44
- 'name' => $name,
45
- 'rank' => $rank,
46
- 'location' => $location,
47
- 'age' => $age,
48
- 'intelligence' => $intelligence
49
- ));
51
+ ``` php
52
+ DB::insert('mytable', array(
53
+ 'name' => $name,
54
+ 'rank' => $rank,
55
+ 'location' => $location,
56
+ 'age' => $age,
57
+ 'intelligence' => $intelligence
58
+ ));
59
+ ```
50
60
51
61
### Grab one row or field
52
62
53
- $account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
54
- $number_accounts = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
63
+ ``` php
64
+ $account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
65
+ $number_accounts = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
66
+ ```
55
67
56
68
### Use a list in a query
57
- DB::query("SELECT * FROM tbl WHERE name IN %ls AND age NOT IN %li", array('John', 'Bob'), array(12, 15));
69
+ ``` php
70
+ DB::query("SELECT * FROM tbl WHERE name IN %ls AND age NOT IN %li", array('John', 'Bob'), array(12, 15));
71
+ ```
58
72
59
- ### Nested Transactions
73
+ ### Log all queries and errors
74
+ ``` php
75
+ // log all queries and errors to file, or ..
76
+ DB::$logfile = '/home/username/logfile.txt';
60
77
61
- DB::$nested_transactions = true;
62
- DB::startTransaction(); // outer transaction
63
- // .. some queries..
64
- $depth = DB::startTransaction(); // inner transaction
65
- echo $depth . 'transactions are currently active'; // 2
66
-
67
- // .. some queries..
68
- DB::commit(); // commit inner transaction
69
- // .. some queries..
70
- DB::commit(); // commit outer transaction
78
+ // log all queries and errors to screen
79
+ DB::$logfile = fopen('php://output', 'w');
80
+ ```
81
+
82
+ ### Nested Transactions
83
+ ``` php
84
+ DB::$nested_transactions = true;
85
+ DB::startTransaction(); // outer transaction
86
+ // .. some queries..
87
+ $depth = DB::startTransaction(); // inner transaction
88
+ echo $depth . 'transactions are currently active'; // 2
89
+
90
+ // .. some queries..
91
+ DB::commit(); // commit inner transaction
92
+ // .. some queries..
93
+ DB::commit(); // commit outer transaction
94
+ ```
71
95
72
- ### Lots More - See: http://www. meekro.com/docs.php
96
+ ### Lots More - See: http://meekro.com/docs
73
97
74
98
75
99
How is MeekroDB better than PDO?
@@ -84,41 +108,51 @@ MeekroDB works. Still, if you need database objects, MeekroDB can do that too.
84
108
The code below escapes your parameters for safety, runs the query, and grabs
85
109
the first row of results. Try doing that in one line with PDO.
86
110
87
- $account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
111
+ ``` php
112
+ $account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
113
+ ```
88
114
89
115
Or how about just one field?
90
116
91
- $created_at = DB::queryFirstField("SELECT created_at FROM accounts WHERE username=%s", 'Joe');
117
+ ``` php
118
+ $created_at = DB::queryFirstField("SELECT created_at FROM accounts WHERE username=%s", 'Joe');
119
+ ```
92
120
93
121
### Work with list parameters easily
94
122
Using MySQL's IN keyword should not be hard. MeekroDB smooths out the syntax for you,
95
123
PDO does not.
96
124
97
- $accounts = DB::query("SELECT * FROM accounts WHERE username IN %ls", array('Joe', 'Frank'));
125
+ ``` php
126
+ $accounts = DB::query("SELECT * FROM accounts WHERE username IN %ls", array('Joe', 'Frank'));
127
+ ```
98
128
99
129
100
130
### Simple inserts
101
131
Using MySQL's INSERT should not be more complicated than passing in an
102
132
associative array. MeekroDB also simplifies many related commands, including
103
133
the useful and bizarre INSERT .. ON DUPLICATE UPDATE command. PDO does none of this.
104
134
105
- DB::insert('accounts', array('username' => 'John', 'password' => 'whatever'));
135
+ ``` php
136
+ DB::insert('accounts', array('username' => 'John', 'password' => 'whatever'));
137
+ ```
106
138
107
139
### Nested transactions
108
140
MySQL's SAVEPOINT commands lets you create nested transactions, but only
109
141
if you keep track of SAVEPOINT ids yourself. MeekroDB does this for you,
110
142
so you can have nested transactions with no complexity or learning curve.
111
143
112
- DB::$nested_transactions = true;
113
- DB::startTransaction(); // outer transaction
114
- // .. some queries..
115
- $depth = DB::startTransaction(); // inner transaction
116
- echo $depth . 'transactions are currently active'; // 2
117
-
118
- // .. some queries..
119
- DB::commit(); // commit inner transaction
120
- // .. some queries..
121
- DB::commit(); // commit outer transaction
144
+ ``` php
145
+ DB::$nested_transactions = true;
146
+ DB::startTransaction(); // outer transaction
147
+ // .. some queries..
148
+ $depth = DB::startTransaction(); // inner transaction
149
+ echo $depth . 'transactions are currently active'; // 2
150
+
151
+ // .. some queries..
152
+ DB::commit(); // commit inner transaction
153
+ // .. some queries..
154
+ DB::commit(); // commit outer transaction
155
+ ```
122
156
123
157
### Flexible debug logging and error handling
124
158
You can log all queries (and any errors they produce) to a file for debugging purposes. You can also add hooks that let you run your own functions at any point in the query handling process.
0 commit comments