Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/flow-flo.../libs/colshrap.../safemysq...
File: README.md
SafeMySQL
[0] Fix | Delete
=========
[1] Fix | Delete
[2] Fix | Delete
SafeMySQL is a PHP class for safe and convenient handling of MySQL queries.
[3] Fix | Delete
- Safe because <b>every</b> dynamic query part goes into the query via <b>placeholder</b>
[4] Fix | Delete
- Convenient because it makes application code short and meaningful, without useless repetitions, making it ''extra'' <abbr title="Don't Repeat Yourself">DRY</abbr>
[5] Fix | Delete
[6] Fix | Delete
This class is distinguished by three main features
[7] Fix | Delete
- Unlike standard libraries, it is using **type-hinted placeholders**, for the **everything** that may be put into the query
[8] Fix | Delete
- Unlike standard libraries, it requires no repetitive binding, fetching and such,
[9] Fix | Delete
thanks to set of helper methods to get the desired result right out of the query
[10] Fix | Delete
- Unlike standard libraries, it can parse placeholders not in the whole query only, but in the arbitary query part,
[11] Fix | Delete
thanks to the indispensabe **parse()** method, making complex queries as easy and safe as regular ones.
[12] Fix | Delete
[13] Fix | Delete
Yet, it is very easy to use. You need to learn only a few things:
[14] Fix | Delete
[15] Fix | Delete
1. You have to **always** pass whatever dynamical data into the query via *placeholder*
[16] Fix | Delete
2. Each placeholder have to be marked with data type. At the moment there are six types:
[17] Fix | Delete
* ?s ("string") - strings (also ```DATE```, ```FLOAT``` and ```DECIMAL```)
[18] Fix | Delete
* ?i ("integer") - the name says it all
[19] Fix | Delete
* ?n ("name") - identifiers (table and field names)
[20] Fix | Delete
* ?a ("array") - complex placeholder for ```IN()``` operator (substituted with string of 'a','b','c' format, without parentesis)
[21] Fix | Delete
* ?u ("update") - complex placeholder for ```SET``` operator (substituted with string of `field`='value',`field`='value' format)
[22] Fix | Delete
* ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing.
[23] Fix | Delete
3. To get data right out of the query there are helper methods for the most used:
[24] Fix | Delete
* query($query,$param1,$param2, ...) - returns mysqli resource.
[25] Fix | Delete
* getOne($query,$param1,$param2, ...) - returns scalar value
[26] Fix | Delete
* getRow($query,$param1,$param2, ...) - returns 1-dimensional array, a row
[27] Fix | Delete
* getCol($query,$param1,$param2, ...) - returns 1-dimensional array, a column
[28] Fix | Delete
* getAll($query,$param1,$param2, ...) - returns 2-dimensional array, an array of rows
[29] Fix | Delete
* getInd($key,$query,$par1,$par2, ...) - returns an indexed 2-dimensional array, an array of rows
[30] Fix | Delete
* getIndCol($key,$query,$par1,$par2, ...) - returns 1-dimensional array, an indexed column, consists of key => value pairs
[31] Fix | Delete
4. For the whatever complex case always use the **parse()** method. And insert
[32] Fix | Delete
[33] Fix | Delete
The rest is as usual - just create a regular SQL (with placeholders) and get a result:
[34] Fix | Delete
[35] Fix | Delete
* ```$name = $db->getOne('SELECT name FROM table WHERE id = ?i',$_GET['id']);```
[36] Fix | Delete
* ```$data = $db->getInd('id','SELECT * FROM ?n WHERE id IN (?a)','table', array(1,2));```
[37] Fix | Delete
* ```$data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);```
[38] Fix | Delete
[39] Fix | Delete
The main feature of this class is a <i>type-hinted placeholders</i>.
[40] Fix | Delete
And it's a really great step further from just ordinal placeholders used in prepared statements.
[41] Fix | Delete
Simply because <b>dynamical parts of the query aren't limited to just scalar data!</b>
[42] Fix | Delete
In the real life we have to add identifiers, arrays for ```IN``` operator, and arrays for ```INSERT``` and ```UPDATE``` queries.
[43] Fix | Delete
So - we need <b>many</b> different types of data formatting. Thus, we need the way to tell the driver how to format this particular data.
[44] Fix | Delete
Conventional prepared statements use toilsome and repeating bind_* functions.
[45] Fix | Delete
But there is a way more sleek and useful way - to set the type along with placeholder itself. It is not something new - well-known ```printf()``` function uses exactly the same mechanism. So, I hesitated not to borrow such a brilliant idea.
[46] Fix | Delete
[47] Fix | Delete
To implement such a feature, no doubt one have to have their own query parser. No problem, it's not a big deal. But the benefits are innumerable.
[48] Fix | Delete
Look at all the questions on Stack Overflow where developers are trying in vain to bind a field name.
[49] Fix | Delete
Voila - with the identifier placeholder it is as easy as adding a field value:
[50] Fix | Delete
[51] Fix | Delete
```php
[52] Fix | Delete
$field = $_POST['field'];
[53] Fix | Delete
$value = $_POST['value'];
[54] Fix | Delete
$sql = "SELECT * FROM table WHERE ?n LIKE ?s";
[55] Fix | Delete
$data = $db->query($sql,$field,"%$value%");
[56] Fix | Delete
```
[57] Fix | Delete
[58] Fix | Delete
Nothing could be easier!
[59] Fix | Delete
[60] Fix | Delete
Of course we will have placeholders for the common types - strings and numbers.
[61] Fix | Delete
But as we started inventing new placeholders - let's make some more!
[62] Fix | Delete
[63] Fix | Delete
Another trouble in creating prepared queries - arrays going to the IN operator. Everyone is trying to do it their own way, but the type-hinted placeholder makes it as simple as adding a string:
[64] Fix | Delete
[65] Fix | Delete
```php
[66] Fix | Delete
$array = array(1,2,3);
[67] Fix | Delete
$data = $db->query("SELECT * FROM table WHERE id IN (?a)",$array);
[68] Fix | Delete
```
[69] Fix | Delete
[70] Fix | Delete
The same goes for such toilsome queries like ```INSERT``` and ```UPDATE```.
[71] Fix | Delete
[72] Fix | Delete
And, of course, we have a set of helper functions to turn type-hinted placeholders into real brilliant, making almost every call to the database as simple as one or two lines of code for all the regular real life tasks.
[73] Fix | Delete
[74] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function