Hallo Habr! Es wurden viele Artikel über gU geschrieben, aber nur sehr wenige Beispiele aus der Praxis. In diesem Artikel möchte ich meine Version der Klasse für die Arbeit mit einer Datenbank (im Folgenden als DB bezeichnet) vorstellen. Dieser Artikel ist nützlich für unerfahrene Programmierer, die diese Technologie gerade beherrschen.
Beachtung! Meine Meinung kann von Ihrer abweichen, daher möchte ich sofort sagen, dass dieser Artikel nicht die ultimative Wahrheit ist und die Implementierung dieser Klasse vom Programmierer und seinen Vorlieben abhängt.
Einführung
Beginnen wir mit der DB-Klasse.
<?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'));
?>
:
, .
<?php
// class DB {...}
//
$db = new DB;
//
echo "<pre>";
print_r($db->getRow('posts'));
?>
. .
<?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'));
?>
:
, , .
Am Ende des Artikels möchte ich mich wiederholen und sagen, dass meine Implementierung nicht perfekt ist , aber dennoch funktioniert diese Klasse und führt ihre Hauptfunktion aus - sie funktioniert mit einer Datenbank. Ich hoffe, dieser Artikel war hilfreich für Sie.
Link zu Github : Klasse DB