website logo
Home Websites Projects Categories About Contact Pricing FAQs Blogs


logo

Home
Websites
Projects
Categories
Services
How we approach
Blogs
Testimonials
About
Contact Us
Pricing
FAQs
Feedback Section
Help

Notifications






Loading...


  • WordPress Website

  • Coded Website

  • Android Web App

php crud operations code with database connectivity


2024-07-25 15:35 · 5 min read · 85633 Views

CRUD operations (Create, Read, Update, Delete) are fundamental for working with databases in PHP. Let?s explore some examples of how to perform these operations using PHP and MySQL:

Create (Insert) Operation:

  • To add data to your database, use an INSERT query.
  • Here?s an example of inserting a new record into a table named employees:
<?php
include 'config.php'; // Include your database connection details

$name = 'John Doe';
$address = '123 Main St';
$salary = 50000;

$sql = "INSERT INTO employees (name, address, salary) VALUES ('$name', '$address', $salary)";
if ($conn->query($sql)) {
echo "Record added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
  • Replace the database connection details (config.php) with your actual credentials.

Read (Select) Operation:

  • To retrieve data from the database, use a SELECT query.
  • Example:
<?php
include 'config.php';

$sql = "SELECT * FROM employees";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row['name'] . " | Salary: " . $row['salary'] . "<br>";
}
} else {
echo "No records found.";
}

$conn->close();
?>

Update Operation:

  • To modify existing data, use an UPDATE query.
  • Example:
<?php
include 'config.php';

$newSalary = 55000;
$employeeId = 1; // Assuming you want to update the record with ID 1

$sql = "UPDATE employees SET salary = $newSalary WHERE id = $employeeId";
if ($conn->query($sql)) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Delete Operation:

  • To remove data, use a DELETE query.
  • Example:
<?php
include 'config.php';

$employeeIdToDelete = 2; // Assuming you want to delete the record with ID 2

$sql = "DELETE FROM employees WHERE id = $employeeIdToDelete";
if ($conn->query($sql)) {
echo "Record deleted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Remember to replace the table name (employees) and field names (name, address, salary) with your actual database schema. Also, consider using prepared statements or PDO for better security.

 

Creating a Database Connection File: 'config.php'

Creating a database connection in PHP is essential for interacting with databases. Below, We are providing examples of how to establish connections using both MySQLi (both object-oriented and procedural) and PDO (PHP Data Objects):

 

MySQLi (Object-Oriented):

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

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

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

echo "Connected successfully!";
?>

 

MySQLi (Procedural):

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

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

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully!";
?>

 

PDO (PHP Data Objects):

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

Remember to replace the placeholders (your_username, your_password, your_database_name) with your actual database credentials. Additionally, choose either MySQLi or PDO based on your preferences and project requirements.

 

TAGS:

Share Now!








POPULAR POSTS

2025-05-28 20:57

Best Website Designing Company in Kashipur, Uttarakhand – The Pro Developers

2025-05-28 20:47

Best Website Design & Development Services in India

2025-05-28 15:12

Best website hosting service to host your website in 2025

2025-05-28 15:06

Common Web Development Mistakes to Avoid – Tips for a flawless website.

2025-05-28 15:03

The Role of SEO in Digital Marketing – How search engines impact business growth.

2025-05-28 14:59

How to Convert Web Apps into Android Apps – A step-by-step guide.

2025-05-28 14:54

Best Practices for User-Friendly Website Navigation – Improve user experience.

2025-05-28 14:51

How to Secure Your Website from Cyber Threats – Essential security measures.

2025-05-28 14:48

Top Web Design Trends in 2025 – Stay ahead with the latest innovations.

2025-05-28 14:45

WordPress vs Custom Websites: Which One is Better? – Pros and cons of each.

2025-05-28 14:40

SEO Strategies for E-Commerce Websites – Tips to boost online store visibility.

2025-05-28 14:34

The Importance of Mobile-Friendly Websites – Why responsive design matters.



Follow Us!





Website © 2023 The Pro Developers | Powered by The Pro Developers Kashipur IN