crud operation via php mysql
crud operation via php mysql
CRUD operations (Create, Read, Update, Delete) allow you to interact with a MySQL database using PHP. Here's an example of how you can perform CRUD operations using PHP and MySQL:
- Establish a database connection:
$host = 'localhost';
$dbName = 'your_database_name';
$username = 'your_username';
$password = 'your_password';
$mysqli = new mysqli($host, $username, $password, $dbName);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
Replace 'localhost'
, 'your_database_name'
, 'your_username'
, and 'your_password'
with your actual database credentials.
- Create a new record (Create operation):
$name = 'John Doe';
$email = '[email protected]';
$age = 25;
$query = "INSERT INTO your_table (name, email, age) VALUES ('$name', '$email', '$age')";
$result = $mysqli->query($query);
if ($result) {
echo "Record created successfully.";
} else {
echo "Error: " . $mysqli->error;
}
Replace 'your_table'
with the name of your database table.
- Retrieve records (Read operation):
$query = "SELECT * FROM your_table";
$result = $mysqli->query($query);
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 "No records found.";
}
This code retrieves all records from the specified table and displays the results.
- Update a record (Update operation):
$id = 1;
$newName = 'Jane Smith';
$query = "UPDATE your_table SET name = '$newName' WHERE id = '$id'";
$result = $mysqli->query($query);
if ($result) {
echo "Record updated successfully.";
} else {
echo "Error: " . $mysqli->error;
}
This code updates the name of the record with the specified ID.
- Delete a record (Delete operation):
$id = 1;
$query = "DELETE FROM your_table WHERE id = '$id'";
$result = $mysqli->query($query);
if ($result) {
echo "Record deleted successfully.";
} else {
echo "Error: " . $mysqli->error;
}
This code deletes the record with the specified ID.
- Close the database connection:
$mysqli->close();
It's important to close the database connection when you're finished with the CRUD operations.
Remember to adjust the code based on your database structure and requirements, including table names and column names. Additionally, consider using prepared statements or input validation to prevent SQL injection attacks.
Related Posts:
- How To Setup LVS (Linux Virtual Server) Load Balancer on Rocky Linux 8.5
- How To Install Magento 2.4 on Rocky Linux 8
- How To Install Docker on Oracle Linux Server 9
- How to Install WHM/cPanel in Almalinux?
- How To Install Ruby on Rails with PostgreSQL on Rocky Linux 8
- How To Install EMQX MQTT Broker on Rocky Linux 8
- How To Install Kamailio SIP Server on Rocky Linux 8
- How to Install and Configure RabbitMQ Server on Rocky Linux 8
- Setup Quick DNS Server On Rocky Linux 8.5 Using Dnsmasq
- 2 Ways To install Ruby On Rocky Linux | RVM
Latest Posts
- Server-Side Scripting: PHP, Node.js, Python – A Detailed Comparison
- Securing Your Website in 2024: Essential Strategies for Online Safety
- The Future of Web Development Technologies: Trends to Watch in 2024
- How Banks Handle Server-Side Operations and Ensure System Security: An Inside Look
- Tips for Writing Clean, Understandable, and Efficient Code: Avoiding Garbage Code
- Tailwind CSS: Revolutionizing Modern Web Design
- Basic Linux Commands for Beginners: A Starter Guide
- Dairy Farming Loan Apply
- BSNL Recharge Plan
- Bijli Bill Mafi Yojana Online Apply
Technical
- DevOps Roadmap
- How To Install and Configure an SNMP on Ubuntu 20.04
- Apple releases iOS 18 Developer Beta 2 with iPhone screen mirroring, RCS toggle,and more
- How to enable SNMP on Ubuntu Linux 18.04 and above
- How to Force HTTPS Using .htaccess (Updated 2024)
- Display All PHP Errors: Basic & Advanced Usage
- PHP alert
- MongoDB loads but breaks, returning status=14
- MongoDB database deleted automatically
- MongoDB all Error Solutions
Category