website logo
Home Websites Projects Categories Courses 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...

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 · 85652 Views
php crud operations code with database connectivity

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-06-06 16:03
Top Website Designing, Website Development, E-Commerce Platforms, SEO & Digital Marketing Company in India – Contact Today!
2025-06-06 15:51
Website Development & Web Design – Get Your Website @ ₹7999/- Only
2025-06-02 17:19
The Best Website Designing and Development Company in Ramnagar, Uttarakhand
2025-06-02 12:47
Premier Freelance Web Design Services at Rs 5,999/- Only - The Pro Developers
2025-06-02 12:36
The Pro Developers: Elevating eCommerce with Exceptional Design & Development
2025-06-02 12:32
Web Development Company: Powering Digital Success with The Pro Developers
2025-06-02 11:00
Affordable Web Designing and Web Development Services Starting from ₹4,999 – The Pro Developers
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.


Follow Us!





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