CRUD НА PHP

CRUD (Create, Read, Update, Delete) — это основные операции, которые выполняются в базах данных. Разработчики часто используют PHP для создания запросов и работа с базами данных. В PHP существует несколько способов взаимодействия с базами данных, включая модули MySQLi и PDO.

Использование MySQLi

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (first_name, last_name, email) VALUES ('John', 'Doe', 'johndoe@mail.com')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
mysqli_close($conn);

Использование PDO

$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO users (first_name, last_name, email) VALUES ('John', 'Doe', 'johndoe@mail.com')";
$conn->exec($sql);
echo "New record created successfully";
$conn = null;

Чтение данных из базы

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, first_name, last_name FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "
";
}
} else {
echo "0 results";
}
mysqli_close($conn);

Обновление данных в базе

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE users SET email='johndoe@yahoo.com' WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);

Удаление данных из базы

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "DELETE FROM users WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);

PHP Crud Operations - Select, Insert, Update, Delete - PHP Tutorial For Beginners - Simplilearn

PHP CRUD -- Create, Read, Update, Delete.

Simple CRUD application with PHP and JSON

CRUD-приложение на чистом PHP

Create CRUD Operation Using AJAX In PHP \u0026 MySQL - Insert Update Delete Using PHP jQuery AJAX

laravel 8 crud step by step

PHP CRUD using Bootstrap Modal -- Complete Project -- AJAX -- JQUERY

PHP and MySQL with CRUD Operations: Create, Read, Update, Delete

CRUD Operation in PHP MySQLi - Select Insert Update/Edit Delete in PHP MySQLi - E-CODEC

Реклама
Новое
Реклама