Skip to content

Commit 4bf4a7f

Browse files
author
modbot
committed
Update 2.1.0
1 parent 4818433 commit 4bf4a7f

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ An easy to use class for Database queries in PHP.
1212
DB::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
1313
DB::getPdo();
1414
DB::setPdo($db);
15-
DB::quote();
15+
DB::quote($string,$remove_quotes=false);
1616
DB::query($query, $params = array());
1717
DB::fetchAll($query);
1818
DB::fetchAll_safe($query);
@@ -105,6 +105,34 @@ First argument is the statement, second argument is an array of parameters (opti
105105

106106
Note: We passed the query into a variable for later re-use.
107107

108+
### Quote
109+
```php
110+
$quoted_string = DB::quote($_GET['id']);
111+
```
112+
113+
```php
114+
# Remove Quotes after quoting, and right before output,
115+
# giving you a similar string as mysql_real_escape_string
116+
$quoted_string = DB::quote($_GET['id'], 1);
117+
```
118+
119+
Escaping in PDO adds quotes around the escaped string, which is an issue if you try doing a **LIKE** query:
120+
121+
```php
122+
# Default Quote adds '' quotes around the field, forcing you to do:
123+
DB::query("SELECT * FROM table WHERE field LIKE ?", ['%'.$input.'%']);
124+
DB::query("SELECT * FROM table WHERE field LIKE ".DB::quote('%'.$input.'%'));
125+
126+
# Removed Quoting, quotes but removes added quotes
127+
DB::query("SELECT * FROM table WHERE field LIKE '%".DB::quote($input,1)."%'";
128+
```
129+
130+
PDO does not provide a way to turn off quotes around escaped strings so, we created a function that simply removes the quotes (first and last characters).
131+
This returns a string similar to the old [mysql_real_escape_string](http://php.net/manual/en/function.mysql-real-escape-string.php) function.
132+
133+
Please note that this requires you to start adding quotes yourself. Escaping is the default when you bind parameters in PDO.
134+
As such, escaping is turned on by default as per the original function (passthrough).
135+
108136
### Fetch and **Safe Fetch**
109137
This is regular returned object. You still need to apply htmlspecialchars yourself.
110138
```php

src/Database.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ public function getPdo()
5353
{
5454
return $this->pdo;
5555
}
56-
public function quote($string)
56+
public function quote($string,$remove_quotes=false)
5757
{
58-
return $this->pdo->quote($string);
58+
$data = $this->pdo->quote($string);
59+
if($remove_quotes) {
60+
$data = substr($data, 1, -1);
61+
}
62+
return $data;
5963
}
6064
public function query($query, $params = array())
6165
{

src/Facade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static function getPdo()
1818
{
1919
return self::$db->getPdo();
2020
}
21-
public static function quote($string)
21+
public static function quote($string,$remove_quotes=false)
2222
{
23-
return self::$db->quote($string);
23+
return self::$db->quote($string,$remove_quotes);
2424
}
2525
public static function query($query, $params = array())
2626
{

0 commit comments

Comments
 (0)