DATABASE PHP

MYSQL CRUD Operations || PHP

mysql

Thus,CURD Operations are create,read,update,delete operations.Php can manipulate the data in database by executing mysql queries. First of all we have to build connection between mysql and php and don’t forget to write database name in mysqli_connect function.

Read Data:

SELECT query use in php to read or get data from mysql database.So,It retruns the multiple rows which obey the query.

Syntax:

SELECT number_of_columns FROM database_name where(condition) // where is not compulsory

Example #1:

<?php
$server = "localhost";
$name = "YOUR Username";
$password = "YOUR Password";
$database="TEMP";

$connection = mysqli_connect($server, $name, $password,$database);

$query = "SELECT * FROM student;"; //SELECT Query

$result=mysqli_query($connection,$query); 

if($row = mysqli_fetch_array($result)){ // Fetching Rows from which return.
    print_r($row);
}else{
echo "The Table is not created";
}
?>

Output:

Array
(
    [0] => 1
    [id] => 1
    [1] => John
    [name] => John
    [2] => Mark
    [father_name] => Mark
    [3] => 98
    [marks] => 98
    [4] => 1234567891
    [phone] => 1234567891
    [5] => Lord's,England
    [address] => Lord's,England
)

Insert Data:

INSERT query is use to insert data in database and It returns 1 for true or 0 for false after execution.It contains names of columns and its respective values.

Syntax:

INSERT INTO table_name(column_name_1,column_name_2) VALUES ("VALUE_1","VALUE_2")

Example #2:

<?php
$server = "localhost";
$name = "Name of User";
$password = "Password of User";
$database="TEMP";

$connection = mysqli_connect($server, $name, $password,$database);

$query = "INSERT INTO student(id,name,father_name,marks,phone,address) VALUES (2,'Billy','Mark',97,123321,'London England');";

$result=mysqli_query($connection,$query);

if($result){
    echo "The Data is inserted";
}else{
    echo "The Data is not inserted";
}
?>

Output:

The Data is inserted

Update Data:

Update query use to update the exists data in the  database.To Update any row,a unique value such as primary key is required.

UPDATE name_of_table SET column_1=value_1,column_2=value_2.....    WHERE any_column=value

Example #3:

<?php
$server = "localhost";
$name = "Your Name";
$password = "Your Password";
$database="TEMP";

$connection = mysqli_connect($server, $name, $password,$database);

$query = "update fashion set style='T-shirt'";

$result=mysqli_query($connection,$query);
 
if($result){
    echo "The Data is Updated";
}else{
    echo "The Data is not Updated";
}
?>

Output:

The Data is Updated

Delete Data:

Delete Statement in php use to delete the row in  database and It require two things,first is table name and second is unique value of that row.

Syntax:

DELETE FROM name_of_table WHERE any_column=unique_value

Example #4:

<?php
$server = "localhost";
$name = "Your Name";
$password = "Your Password";
$database="TEMP";

$connection = mysqli_connect($server, $name, $password,$database);

$query = "DELETE FROM student where id=1;";

$result=mysqli_query($connection,$query);
 
if($result){
    echo "The ROW is DELETED";
}else{
    echo "The ROW is not DELETED";
}
?>

Output:

The ROW is DELETED

Note: There are many Compilers you can use to run  the HTML.CSS,JAVASCRIPT and PHP codes like Brackets, visual studio and Atom . The best and recommended editor is Visual Studio if you have a System with Sufficient requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *