PHP PDO mysql class vs Memcache
- 19 November 2014
- Volodymyr Hodiak
- Development
- Administration
- 5410
I've decided to share with you a driver that we use to work with MySQL database. This class is a PDO extension and includes ready-to-use functions to perform CRUD. It uses the singleton pattern. The primary task is to reduce the number of mistakes in routine operations and reduce the amount of lines of code.
Connection
It doesn't take more than coping it to the folders with classes, and if you follow psr-0, then the autoload has to download it.
Configure connection with database
There's the $_config file in the class constructor. You should replace it with yours or insert your values.
The content of the config file is clear. There's no need to describe it.
$_config = array( 'host' => 'localhost', 'username' => 'root', 'password' => '', 'db' => 'you db name', 'memcached' => false, 'mc_host' => 'localhost' );
Mind if you want to use Memcache. If so write the mc_host. If you use the memory from the local server, then specifying the localhost is enough.
Usage
SELECT
$db = DB::instance(); $r = $db->select("select id,name,is_folder from menu order by id asc")->all(); Result: Array ( [0] => Array ( [id] => 1 [name] => About Project [title] => About Project
) [1] => Array ( [id] => 2 [name] => History [title] => History
) ) $r = $db->select("select name,is_folder from menu where id=1")->row(); Result: ( [name] => About Project
[title] => About Project
) $r = $db->select("select name from menu where id=1")->row('name'); Result: About Project
Also, the select method has another parameter $debug. If true | 1, then before running a query, SQL dump is displayed.
INSERT
Before:
INSERT INTO `menu` (`id`, `name`, `title`) VALUES (NULL, 'About Project', 'About Project
т');
After:
$db->insert( 'menu', array( 'name' => 'About Project', 'title'=> 'About Project', ) ); The probability of error is reduced. Everything is screened automatically. UPDATE UPDATE `menu` SET `title` = 'Історія 1' WHERE `menu`.`id` =2; $db->update('menu', array('title'=> 'Історія 1'), 'id=2');
The difference is not obvious when the number of fields is small. But if you add some data, for example, into the $_POST['menu'] array, then you can add fields, and you don't have to worry about the handler.
$db->update('menu', $_POST['menu'], 'id=2');
Feel the difference?!
DELETE
DELETE FROM `menu` WHERE `menu`.`id` = 2 $db->delete('menu', 'id=2'); $db->delete('menu', 'id=2 limit 1');
I could have used the limit method, but I don't know if there's any need for it.
MEMCACHE
If you need to use Memcache, the following code fragment is enough:
$db = DB::instance(); $db->useCache(1, 3600); $r = $db->select("select id,name,title from menu order by id asc")->all(); або $db = DB::instance()->useCache(1, 3600); $r = $db->select("select id,name,title from menu order by id asc")->all(); You can change the cache retention period.
This is just the list of basic methods in the class. To find out more, open the code and learn about all abilities.
Thank you for your attention. Like the article, if you find it useful.
And the last thing – the code on GitHub.