PHP Tutorials
- What is MySqli
- mysql query
- mysql query example
- InnoDB
- mysql column Type
- CRUD Example
- Connection Using Function
- mysql keys
- SELECT
- WHERE
- UPDATE
- Count no of Rows
- ALIAS
- AND, AND & OR
- BETWEEN
- COMPARISON OPERATOR
- DELETE
- DELETE LIMIT
- DISTINCT
- EXISTS
- FROM
- GROUP BY
- HAVING
- IN
- INTERSECT
- IS NULL & IS NOT NULL
- LIKE
- NOT
- ORDER BY
- SELECT LIMIT
- SUBQUERY
- TRUNCATE
- UNION && UNION ALL
- Concat & Group_Concat
- mysql Function
- Mysql Insert Id
- MySql Aggregate Function
- Mysql Join
- JOIN in MySql
- Trigger
- Procedure
- Transaction
- views
- Index
- SQL Injection
- Normalization
- Query Bind
- Interview Questions
Important Link
Insert Query :-
Insert Data into database.
Example (MySQLi procedural):-
<?php
include 'db.php';
$ename = "mahtab";
$eaddress = "delhi";
$emobile = "7838897299";
$qry = "insert into employee (empname,address,mobile) values ('$ename','$eaddress','$emobile')";
$result = mysqli_query($con, $qry);
if($result > 0)
{
header("Location:employee.php?msg= record inserted Successfully.");
}
else
{
header("Location:employee.php?msg= Sorry! Please fillup the form again.");
}
?>
include 'db.php';
$ename = "mahtab";
$eaddress = "delhi";
$emobile = "7838897299";
$qry = "insert into employee (empname,address,mobile) values ('$ename','$eaddress','$emobile')";
$result = mysqli_query($con, $qry);
if($result > 0)
{
header("Location:employee.php?msg= record inserted Successfully.");
}
else
{
header("Location:employee.php?msg= Sorry! Please fillup the form again.");
}
?>
Insert id into table using mysqli_insert_id method.
Example :-
Select Data from database.
Example :-
Setup our query
$query = "SELECT * FROM usertable";
//Run the Query
$result = mysqli_query($con,$query);
//If the query returned results, loop through
// each result
if($result)
{
while($row = mysqli_fetch_array($result))
{
$name = $row["name"];
echo "Name: " . $name;
}
}
$query = "SELECT * FROM usertable";
//Run the Query
$result = mysqli_query($con,$query);
//If the query returned results, loop through
// each result
if($result)
{
while($row = mysqli_fetch_array($result))
{
$name = $row["name"];
echo "Name: " . $name;
}
}
Select Data from database with where clause.
Example :-
$id = $_GET['userid'];
$query = "SELECT * FROM usertable where id="$id"";
$result = mysqli_query($con,$query);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$name = $row["name"];
echo "Name: " . $name;
}
}
$query = "SELECT * FROM usertable where id="$id"";
$result = mysqli_query($con,$query);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$name = $row["name"];
echo "Name: " . $name;
}
}
Delete all Data from database.
Example :-
$query = "delete FROM usertable";
$result = mysqli_query($con,$query);
$result = mysqli_query($con,$query);
Delete particular Data from database with where clause.
Example :-
$id = $_GET['id'];
$query = "delete FROM usertable where id='$id'";
$result = mysqli_query($con,$query);
$query = "delete FROM usertable where id='$id'";
$result = mysqli_query($con,$query);
Update Record in Database.
Example :-
<?php
include 'db.php';
if(isset($_POST['submit'])!=="")
{
$mid = $_POST['id'];
$menuname = $_POST['menuname'];
$res = mysqli_query($con, "update menu set menu_name='$menuname' where id='$mid'");
if($res > 0)
{
header("Location:updatemenu.php?msg= Menu Name updated Successfully.");
}
else {
header("Location:updatemenu.php?msg= Sorry! Menu Name Not updated.");
}
}
?>
include 'db.php';
if(isset($_POST['submit'])!=="")
{
$mid = $_POST['id'];
$menuname = $_POST['menuname'];
$res = mysqli_query($con, "update menu set menu_name='$menuname' where id='$mid'");
if($res > 0)
{
header("Location:updatemenu.php?msg= Menu Name updated Successfully.");
}
else {
header("Location:updatemenu.php?msg= Sorry! Menu Name Not updated.");
}
}
?>
ALTER TABLE :-
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
ALTER TABLE Syntax To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype
Delete a column.
ALTER TABLE Persons
DROP COLUMN DateOfBirth
create database connection.
$host = "localhost";
$username ="itechtuto";
$password = "itech*121";
$db_name = "itech";
$con = mysqli_connect($host,$username,$password)or die(mysqli_error());
mysqli_select_db($con,$db_name)or die(mysqli_error($con));
$username ="itechtuto";
$password = "itech*121";
$db_name = "itech";
$con = mysqli_connect($host,$username,$password)or die(mysqli_error());
mysqli_select_db($con,$db_name)or die(mysqli_error($con));