Unit 3 Last Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1.

Display limited records from employee table


Source code:

<?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:

2. To take user input of employee ID and update employee


salary
source code:

<?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::

Before execution once check the salary of the fourth employee(id=4).


After execution check the salary of employee id =4
3. to release cursor memory at the end of SELECT statement.

Source code::
<?php

$link = mysqli_connect('localhost','root','','csestaff');

echo '<br/>Memory Usage Before Query = '.memory_get_usage();

$resultResource = mysqli_query($link, 'SELECT * FROM staffdetails');

echo '<br/>Memory Usage after Query = '.memory_get_usage();

$result = mysqli_fetch_assoc($resultResource);

echo '<br/><br/>Memory Usage Before Free Result = '.memory_get_usage();

mysqli_free_result($resultResource);

echo '<br/>Memory Usage After Free Result = '.memory_get_usage();

?>

Output:

4. to display 10 records per page


source page:
<?php
$con=mysqli_connect('localhost', 'root', '','csestaff') or
die(mysqli_error($con));
if($con)
{
echo "database connected succesfully"."<br>";
}
$page=$_GET["page"];
if($page=="" || $page=="1")
{
$page1=0;
}
else
{
$page1=($page*10)-10;
}
$data=mysqli_query($con,"select * from staffdetails order by id ASC
limit $page1,10;");
?>
<center>
<h2>Employees Database</h2>
<table border=”1”>
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>SALARY</th>
</tr>
<?php
while($row=mysqli_fetch_array($data,MYSQLI_NUM))
{
?>
<tr>
<td><?php echo $row['0']; ?></td>
<td><?php echo $row['1']; ?></td>
<td><?php echo $row['2']; ?></td>
<td><?php echo $row['3'] ;?></td>
</tr>
<?php
}
?>
</table>
</center>
</div>
<?php
$data1=mysqli_query($con,"select * from staffdetails ;");
$cou=mysqli_num_rows($data1);
$a=ceil($cou/10); echo
"<br>"; echo "<br>";
echo "<br>";
for($b=1;$b<=$a;$b++)
{
?>
<a href="paging.php?page=<?php echo $b; ?>" style="text-
decoration:none" ><?php
echo $b." ";?></a>
<?php
}
?>

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:

You might also like