CRUD — акроним, обозначающий четыре базовые функции, используемые при работе с базами данных:
- Создание (англ. create)
- Чтение (read)
- Модификация (update)
- Удаление (delete)
<?php
$host = "localhost";
$dbName = "";
$username = "";
$password = "";
try {
$DBH = new PDO("mysql:host=$host;dbname=$dbName", $username, $password); $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $DBH->exec("set names utf8");} catch(PDOException $e) {
echo $e->getMessage(); file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);} <?php
require_once 'connect.php';
$variableOne = "";
$variableTwo = "";
$variableThree = "";
$stmfCreate = $DBH->prepare("INSERT INTO table_name (variable_one, variable_two, variable_three) VALUES (:variableOne, :variableTwo, :variableThree)");
try {
$stmfCreate->bindParam(':variableOne', $variableOne); $stmfCreate->bindParam(':variableTwo', $variableTwo); $stmfCreate->bindParam(':variableThree', $variableThree); $stmfCreate->execute();} catch(PDOException $e) {
echo $e->getMessage(); file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);} <?php
require_once 'connect.php';
$id = "";
$today = date("Y-m-d H:i:s"); // Format MySQL DATETIME
$stmtRead = $DBH->query("SELECT * FROM table_name WHERE id = '$id' AND created_at = '$today'");
$stmtRead->setFetchMode(PDO::FETCH_ASSOC);
// $rowCount = $stmtRead->rowCount();
foreach ($stmtRead as $row) {
$variableOne = $row['variable_one']; $variableTwo = $row['variable_two']; $variableThree = $row['variable_three'];} <?php
require_once 'connect.php';
$variableOne = "";
$variableTwo = "";
$variableThree = "";
$stmfUpdate = $DBH->prepare("UPDATE table_name SET variable_one = (:variableOne), variable_two = (:variableTwo), variable_three = (:variableThree)");
try {
$stmfUpdate->bindParam(':variableOne', $variableOne); $stmfUpdate->bindParam(':variableTwo', $variableTwo); $stmfUpdate->bindParam(':variableThree', $variableThree); $stmfUpdate->execute();} catch(PDOException $e) {
echo $e->getMessage(); file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);} <?php
require_once 'connect.php';
$id = "";
try {
$stmfDelete = $DBH->prepare("DELETE FROM table_name WHERE id = :id"); $stmfDelete->bindParam(':id', $id); $stmfDelete->execute();} catch(PDOException $e) {
echo $e->getMessage(); file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);}