Chapter - 5 Database Operations Marks-14: Content Outline
Chapter - 5 Database Operations Marks-14: Content Outline
Database operations
Marks- 14
Content outline:
5.1 Introduction to MySQL-create a database
5.2 Connecting to MySQL database- MySQL database server from PHP
5.3 Database operations- Insert data, retrieving the query result
5.4 Update and delete operations on table data
Example:
<?php
// Create connection
$conn = mysqli_connect("localhost","root","","stud");
// Check connection
if($conn)
echo "Connection established";
else
echo "Connection fail";
?>
$conn->close();
?>
$dbname='Student';
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
echo "<br>List of tables present in database-".$dbname;
if($result)
{
while($row=mysqli_fetch_row($result))
{
echo "<br>".$row[0];
}
}
else
{
<table >
<thead>
Q. Write a PHP script to read account information for customer name, account_no, account_type,
branch_name, city, amount from Account table and display all these information in table format.
Ans:
<html>
<body>
<?php
$conn = mysqli_connect("localhost","root","","Account");
// Check connection
if($conn)
<table border=’1’>
<tr><th>Name</th><th>Account No</th><th>Account
Type</th><th>Branch</th><th>City</th><th>Amount</th></tr>
<?php
$rows = $result->num_rows;
for($k=0;$k<$rows;$k++)
{
$result->data_seek($k);
$row = $result->fetch_array(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><td> <?php echo $row[4] ?></td><td>
<?php echo $row[5] ?> </td></tr>
<?php
}
$conn->close();
?>
</tbody>
</table>
</body>
</html>