Small class for working with databases (PDO)

Hello, Habr! There are a lot of articles written about PDO, but very few real-life examples. In this article, I want to present my version of the class for working with a database (hereinafter referred to as the DB). This article will be useful for novice programmers who are just mastering this technology.





Attention! My opinion may differ from yours, so I want to say right away that this article is not the ultimate truth and the implementation of this class depends on the programmer and his preferences.





Introduction

Let's start with the DB class.





<?php 

	// use PDO -     ,   
	//  namespace .     
	//       -  
	use PDO;

	class DB
	{

		public function __construct()
		{
			
		}

	}

?>
      
      



, , , .





.





<?php 

	use PDO;

	class DB
	{
		// ,   PDO
		private $db;

		public function __construct()
		{
			//  dbinfo.php    
			//   
			$dbinfo = require 'path/to/dbinfo.php';
			// 
			$this->db = new PDO('mysql:host=' . $dbinfo['host'] . ';dbname=' . $dbinfo['dbname'], $dbinfo['login'], $dbinfo['password']);
		}

	}

?>
      
      



, . , SQL .





<?php 

	use PDO;

	class DB
	{
		//   PDO
		private $db;

		//   
		public function __construct()
		{
			$dbinfo = require 'path/to/dbinfo.php';
			$this->db = new PDO('mysql:host=' . $dbinfo['host'] . ';dbname=' . $dbinfo['dbname'], $dbinfo['login'], $dbinfo['password']);
		}

		//   
		public function query($sql, $params = [])
		{
			
		}

	}

?>
      
      







query

, :





" ?"









:





  • $sql



    - SQL .





  • $params



    - - .









?





, , , . :





<?php 

	$sql = "SELECT * FROM `table` WHERE id = :id";
	$params = [
		'id' => 5
	];

?>
      
      



: " ?" - , SQL .









:





$params



.





, . :





<?php 

	use PDO;

	class DB
	{
		//   PDO
		private $db;

		//   
		public function __construct()
		{
			$dbinfo = require 'path/to/dbinfo.php';
			$this->db = new PDO('mysql:host=' . $dbinfo['host'] . ';dbname=' . $dbinfo['dbname'], $dbinfo['login'], $dbinfo['password']);
		}

		//   
		public function query($sql, $params = [])
		{
			//  
			$stmt = $this->db->prepare($sql);
			
			//     
			//   
			if ( !empty($params) ) {
				foreach ($params as $key => $value) {
					$stmt->bindValue(":$key", $value);
				}
			}
			
			//  
			$stmt->execute();
			//  
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}

	}

?>
      
      



,





, SQL, . , , :





  • getAll()



    - ,





  • getRow()



    - ,









, .





<?php 

	use PDO;

	class DB
	{
		//   PDO
		private $db;

		//   
		public function __construct()
		{
			$dbinfo = require 'path/to/dbinfo.php';
			$this->db = new PDO('mysql:host=' . $dbinfo['host'] . ';dbname=' . $dbinfo['dbname'], $dbinfo['login'], $dbinfo['password']);
		}

		//   
		public function query($sql, $params = [])
		{
			//  
			$stmt = $this->db->prepare($sql);
			
			//     
			//   
			if ( !empty($params) ) {
				foreach ($params as $key => $value) {
					$stmt->bindValue(":$key", $value);
				}
			}
			
			//  
			$stmt->execute();
			//  
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}

		public function getAll($table, $sql = '', $params = [])
		{
			return $this->query("SELECT * FROM $table" . $sql, $params);
		}

		public function getRow($table, $sql = '', $params = [])
		{
			$result = $this->query("SELECT * FROM $table" . $sql, $params);
			return $result[0]; 
		}

	}

?>
      
      



, , .





, , .





:





  • getOne()



    -





  • getCol()



     - 1





  • ..





. "".





. , posts. .





DB



. , , .





dbinfo.php - .





<?php 

	//    
	return [
		'host' => '127.0.0.1',
		'dbname' => 'test',
		'login' => 'root',
		'password' => ''
	];

?>
      
      



, . , test



.





<?php
	
	// class DB {...} 
	//  
	$db = new DB;

	//    
	echo "<pre>";
	print_r($db->getAll('posts'));

?>
      
      



:





data from the database

, .





<?php
	
	// class DB {...} 
	//  
	$db = new DB;

	//    
	echo "<pre>";
	print_r($db->getRow('posts'));

?>
      
      



data from the database

. .





<?php
	
	// class DB {...} 
	//  
	$db = new DB;

	//    
	echo "<h1></h1><pre>";
	print_r($db->getAll('posts'));
	echo "</pre><h1></h1><pre>";
	$params = [
		'title' => '  PHP', 
		'author' => '  PHP'
	];
	$db->query('INSERT INTO `posts` ( title, author ) VALUES ( :title, :author )', $params);
	print_r($db->getAll('posts'));
?>
      
      



:





data from the database

, , .





At the end of the article, I want to repeat myself and say that my implementation is not perfect , but still this class works and performs its main function - it works with a database. I hope this article was helpful for you.









Link to github : class DB








All Articles