website logo
Home Websites Categories Projects About Contact Pricing FAQs Blogs Live Classes


logo

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

Notifications






Loading...

website logo

The Pro Developers

Contact for website designing and development services.



Click on the whatsapp button to chat with
The Pro Developers.

Pro Developers

php crud operations code with database connectivity

2024-07-25 15:35 ยท 5 min read ยท 85762 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-10-25 15:25
๐ŸŒŸ Why Choose Us
2025-10-25 15:24
๐Ÿค User-Friendly Web Design
2025-10-25 15:23
๐Ÿง‘โ€๐Ÿ’ผ Web Design for Small Businesses
2025-10-25 15:21
๐Ÿฅ‡ Top Web Design Agency in India
2025-10-25 15:20
๐Ÿ† Best Web Design Company in India
2025-10-25 15:19
๐ŸŒ Web Development & Design
2025-10-25 15:18
๐Ÿ–ฅ๏ธ WordPress Website Design
2025-10-25 15:16
๐Ÿ”„ Website Redesign Services
2025-10-25 15:15
๐Ÿ“ฑ Mobile-Friendly Website Design
2025-10-25 15:14
๐ŸŽจ Creative Web Design Solutions
2025-10-25 15:13
๐Ÿ’ธ Affordable Web Design
2025-10-25 15:12
๐Ÿ›’ E-commerce Website Design


Follow Us!





Website ยฉ 2023 The Pro Developers | Powered by The Pro Developers Kashipur IN