Unit 3 Last Programs
Unit 3 Last Programs
Unit 3 Last Programs
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "csestaff");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM staffdetails LIMIT 5";
if($result = mysqli_query($link, $sql))
{
if(mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Output:
<?php
$id=4;
$con=mysqli_connect('localhost', 'root', '','csestaff') or die(mysqli_error($con));
if($con)
{
echo "database connected succesfully"."<br>";
}
$check=mysqli_query($con,"update staffdetails set salary=300000 where id=$id;");
if($check)
{
echo "Table updated succesfully"."<br>";
}
else
{
echo "oops!error occured";
}
?>
OUTPUT::
Source code::
<?php
$link = mysqli_connect('localhost','root','','csestaff');
$result = mysqli_fetch_assoc($resultResource);
mysqli_free_result($resultResource);
?>
Output:
Output:
Click on 1
Click on 2
Click on 3
5. To take user input of employee ID and delete an employee
record from employee table.
Source code:
<?php
$id=2;
$con=mysqli_connect('localhost', 'root', '','csestaff') or
die(mysqli_error($con));
if($con)
{
echo "database connected succesfully"."<br>";
}
$check=mysqli_query($con,"DELETE from staffdetails where id=$id;");
if($check)
{
echo "Table updated succesfully"."<br>";
}
else
{
echo "oops!error occured";
}
?>
Output: