What is crud Operation in php?
Or
Write a program where php program connect MySQLi database?
Today we know about crud Operation and how to connect MySQLi database with php program.
CRUD OPERATION
- Crud Operation stands for create, read (select), update and delete.First of all we create a MySQLi database in phpmyadmin. so we have 'mydb' database and table name 'student'.now create a connection of database php file connection.php and write this code given below:-
Example : 'connection.php',
<?php
$hostname="localhost";
$username="root";
$password="";
$dbname="mydb";
$con=mysqli_connect($hostname,
$username,$password,$dbname);
if($con){
echo"connected";
}
else{
echo"not connected";
}
?>
<?php
create database with php:
create database with php command ,code given below:-
Example:
$sql = 'CREATE Database mydb';
- now we insert data in database table('student').code given below:
Example: 'insert.php'
if(isset($_POST['login'])){
$gmail=$_POST['gmail'];
$password=$_POST['password'];
$query="insert into `student`(`gmail`
;`password`)
values('$gmail','$password')";
$result=mysqli_query($con,$query);
if($result){
echo"inserted";
}
else{
echo"not inserted";
}
- now we perform read(select) crud operation.here we fetch data from our database so this code given below:-
Example: 'read.php'
<?php
include ("connection.php");
include ("insert.php");
include ("update.php");
include ("delete.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>crud operation</title>
<table border="0" bgcolor="aqua"
cellpadding="10" >
<thead>
DATABASE of Student
<tr>
<th>Gmail</th>
<th>password</th>
</tr>
</thead>
<tbody>
<?php
$sql="SELECT * FROM userdata";
//select data from database
$result=mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc
($result)) {
echo "<tr>
<td> ".$row['gmail']."</td>
<td>".$row['password']."</td>
</tr>";
}
?>
- If we want update data from database record then we write this code which given below:
Example:'update.php'
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cicst";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed:".$conn->
connect_error);
}
//update data in record$sq ="UPDATE MyGuests SET
lastname='Doe' WHERE id=2";
if ($conn->query($sq) === TRUE) {
echo "Record updated successfully";
} else {
echo"Error updating:".$conn->error;
}
$conn->close();
?>
- If we want delete data from database record then we write this code which given below:
Example:'delete.php'
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cicst";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed:".$conn->
connect_error);
}
//delete data from record in mysql
$sql ="DELETE FROM MyGuests
WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo"Error deleting :".$conn->error;
}
$conn->close();
Comments
Post a Comment