0% found this document useful (0 votes)
17 views

How To Crud PHP

Uploaded by

ahmad irwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

How To Crud PHP

Uploaded by

ahmad irwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

how to crud in php

ChatGPT
ChatGPT

CRUD (Create, Read, Update, Delete) operations are fundamental in web development for
interacting with databases. Here’s a basic guide on how to perform CRUD operations in PHP:

1. Setting Up Your Database

First, you need to set up your database and table(s). Here's a simple example using MySQL:

sql
Copy code
CREATE DATABASE my_database;
USE my_database;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT
);

2. Connecting to the Database

In your PHP script, establish a connection to your MySQL database using mysqli or PDO.
Here’s an example using mysqli:

php
Copy code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

3. Performing CRUD Operations

Create (Insert)

To insert new data into the database:

php
Copy code
$name = "John Doe";
$email = "[email protected]";
$age = 30;

$sql = "INSERT INTO users (name, email, age) VALUES ('$name', '$email',
$age)";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

Read (Select)

To fetch data from the database:

php
Copy code
$sql = "SELECT id, name, email, age FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email:
" . $row["email"]. " - Age: " . $row["age"]. "<br>";
}
} else {
echo "0 results";
}

Update

To update existing data in the database:

php
Copy code
$id = 1;
$newAge = 35;

$sql = "UPDATE users SET age=$newAge WHERE id=$id";

if ($conn->query($sql) === TRUE) {


echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

Delete

To delete data from the database:

php
Copy code
$id = 2;

$sql = "DELETE FROM users WHERE id=$id";

if ($conn->query($sql) === TRUE) {


echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

4. Closing the Database Connection

After performing database operations, it’s important to close the database connection:

php
Copy code
$conn->close();

Important Notes:

 Security: Use prepared statements and parameterized queries to prevent SQL


injection attacks.
 Error Handling: Implement robust error handling to manage database errors and
exceptions.
 Best Practices: Consider using an ORM (Object-Relational Mapping) library like
Doctrine or an MVC (Model-View-Controller) framework like Laravel for more
structured and scalable applications.

This basic guide covers the essentials of CRUD operations in PHP. Adjust the database
credentials ($servername, $username, $password, $dbname) and SQL queries according to
your specific setup and requirements.

You might also like