mysqli-examples

Following examples shows that how to use mysqli functions to insert, update, delete and select records from database.

mysqli, here the letter ‘i’ stands for improved version.

Example 1 : Insert record to mysql database through php using MySQLi function.

Step 1 : create a following html file.

<html>
<body>
<form name=f1 action="mysqli_ins.php" method="POST">
Enter Emp Code: <input type=text name=t1> <br>
Enter Emp Name: <input type=text name=t2> <br>
Enter Emp Salary: <input type=text name=t3> <br>
<input type=submit value="Submit Data">
</form>
</body>
</html>

Step 2: Now create “mysqli_ins.php” with following code in it.

<?php

$ec=$_POST['t1'];
$en=$_POST['t2'];
$sal=$_POST['t3'];

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emp_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection issue: " . $conn->connect_error);
} 

$sql = "INSERT INTO emp_details (ecode, ename, sal)
VALUES ('$ec', '$en', '$sal')";

if ($conn->query($sql) === TRUE) {
    echo "New record added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Step 3: Now run html file and enter a record in it and click on submit button to insert new record to table.

Example 2: Below example shows updating an existing records.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emp_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection issue: " . $conn->connect_error);
} 

$sql = "UPDATE emp_details SET ecode='e002' WHERE ename='amit'";

if ($conn->query($sql) == TRUE) {
    echo "One record updated successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Example 3: Below example shows that old / existing record can be deleted.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emp_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection issue: " . $conn->connect_error);
} 

$sql = "DELETE from emp_details  WHERE ecode='E91'";

if ($conn->query($sql) === TRUE) {
    echo "One record Deleted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Example 4: Select query example.

<?php
$con=mysqli_connect("localhost","root","","emp_db");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM emp_details");

echo "<center>";
echo "<h2>Select Query </h2>";
echo "<table border=1>";
echo "<tr bgcolor=khaki>";
echo "<th> Employee code </th>";
echo "<th> Employee Name </th>";
echo "<th> Salary </th>";
echo "</tr>";

while($row = mysqli_fetch_array($result))
  {
echo "<tr>";
echo "<td>" .$row['ecode']. "</td>";
echo "<td>" .$row['ename']. "</td>";
echo "<td>" .$row['sal']. "</td>";
}
echo "</table>";
echo "<center>";
?>

 

Share
Share
Scroll to Top