Tuesday 27 February 2024

U-WPP-703 (Unit IV: Chapter 8: Database Connectivity with MySQL )

Chapter 8: Database Connectivity with MySQL

8.1 Introduction to RDBMS

8.2 Connection with MySQL Database

8.3 Performing basic database operation(DML) (Insert, Delete, Update, Select)

8.4 Setting query parameter

8.5 Executing query

8.6 Join

8.7 Web Hosting steps

8.1 Introduction to RDBMS:

A database is a separate application that stores a collection of data. Each database has one or more features for creating, accessing, managing, searching and replicating the data it holds.

Other kinds of data stores or data structures can also be used, such as files on the file system or large hash tables in memory but data fetching and writing would not be so fast and easy with those type of systems.

We use relational database management systems (RDBMS) to store and manage large amount of data. This is called relational database because all the data is stored into different tables and relations are established using primary keys or other keys known as Foreign Keys.

A Relational DataBase Management System (RDBMS) is a software which:

 Enables you to implement a database with tables, columns and indexes.

 Guarantees the Referential Integrity between rows of various tables.

 Updates the indexes automatically.

 Interprets an SQL query and combines information from various tables.

Important terms in RDBMS:

 Database: A database is a collection of tables, with related data.

 Table: A table is a matrix with data. A table in a database looks like a simple spreadsheet.

 Column: One column (data element) contains data of one and the same kind, for example the column

rollno.

 Row: A row (= tuple, entry or record) is a group of related data.

 Redundancy: Storing data twice, redundantly to make the system faster.

 Primary Key: A primary key is unique. A key value cannot occur twice in one table. With a key, you can only find one row.

 Foreign Key: A foreign key is the linking pin between two tables.

 Compound Key: A compound key (composite key) is a key that consists of multiple columns, because one column is not sufficiently unique.

 Index: An index in a database resembles an index at the back of a book.

 Referential Integrity: Referential Integrity makes sure that a foreign key value always points to an

existing   row.

MySQL Database:

MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is developed, marketed and supported by MySQL AB, which is a Swedish company.

MySQL is becoming so popular because of many good reasons:

 MySQL is released under an open-source license. So you have nothing to pay to use it.

 MySQL is a very powerful program in its own right. It handles a large subset of the functionality of the most expensive and powerful database packages.

 MySQL uses a standard form of the well-known SQL data language.

 MySQL works on many operating systems and with many languages including PHP, PERL, C, C++,

JAVA, etc.

 MySQL works very quickly and works well even with large data sets.

 MySQL is very friendly to PHP, the most appreciated language for web development.

 MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoretical limit of 8 million terabytes (TB).

 MySQL is customizable. The open-source GPL license allows programmers to modify the MySQL software to fit their own specific environments.

8.2 Connection with MySql Database:

mysqli_connect() Function:

The mysqli_connect() function in PHP is used to connect you to the database. In the earlier version of the connection mysql_connect() was used for connection and then there comes mysqli_connect() where i means improved version of connection and is more secure than mysql_connect().

Syntax:

mysqli_connect ( "host", "username", "password", "database_name" )

Example:

mysqli_connect("localhost","root","","login");

In this example the mysqli_connect() function connects to "localhost" with the user name "root", an empty password, and selects the "login" database as the default database.

8.3 and 8.4 Performing basic database operation(DML) (Insert, Delete, Update, Select) and Setting query parameter 

Preforming Insert operation on database:

Consider the following script for html interface to accept data from user.

<html>

<head>

<title> Insert operation on database </title>

</head>

<body>

<div id="Logfrm">

<form action="insertsucess.php" method="POST">

<p><label>Roll No: </label>

<input type="text" name="rollno" id="rollno" disabled />

</p>

<p><label>Student Name: </label>

<input type="text" name="studname" id="studname" />

</p>

<p><label>Class: </label>

<input type="text" name="class" id="class" />

</p>

<p><label>Permenant Address: </label>

<input type="text" name="padd" id="padd" />

</p>

<p>

<input type="submit" name="save" id="save" Value="Save" />

</p>

</div>

</form>

</body>

</html>

Consider the following php code to perform insert operation on database “insertsucess.php”.

<?php

//$rollno=$_POST["rollno"];

$studname=$_POST["studname"];

$class=$_POST["class"];

$padd=$_POST["padd"];

$con=mysqli_connect("localhost","root","","login");

if ($con==True){

$query="INSERT INTO `studinfo`( `studname`, `class`, `padd`) VALUES ('$studname','$class','$padd')";

mysqli_query($con,$query);

echo"Record added sucessfully!";

}

else{

echo "fail";

}

mysqli_close($con);

?>

Preforming delete operation on database:

Consider the following php script to select record for delete operation. “remove.php”

<?php

$con=mysqli_connect("localhost","root","","login");

$query="SELECT `rollno` FROM `studinfo`";

$res=mysqli_query($con,$query);

echo "Select Roll number : &nbsp&nbsp&nbsp ";

?>

<html>

<body>

<form action="removed.php" method="get">

<select name="rno">

<?php

$i=0;

while($result=mysqli_fetch_array($res)){

?>

<option> <?php echo $result['rollno'];?></option>

<?php

}

?>

</select>

<input type="submit" name ="btndelete" value="Delete">

</form>

<body>

<html>

Consider the following php script for delete selected record. “removed.php”

<?php

$rollno=$_GET['rno'];

$con=mysqli_connect("localhost","root","","login");

$query="DELETE FROM `studinfo` WHERE `rollno`=$rollno";

$res=mysqli_query($con,$query);

echo "Record deleted! ";

echo "</br>Delete another Record <a href=remove.php>click here </a>";

?>

Preforming update operation on database:

Consider the following php script to select record for update operation. “update.php”

<?php

$con=mysqli_connect("localhost","root","","login");

$query="SELECT `rollno` FROM `studinfo`";

$res=mysqli_query($con,$query);

echo "Select Roll number for update details : &nbsp&nbsp&nbsp ";

?>

<html>

<body>

<form action="showforupdate.php" method="post">

<select name="rno">

<?php

$i=0;

while($result=mysqli_fetch_array($res)){

?>

<option> <?php echo $result['rollno'];?></option>

<?php

}

?>

</select>

<input type="submit" name ="btnshow" value="For Update">

</form>

<body>

<html>

Consider the following php script to show selected record for updating details . “showforupdate.php”

<?php

$rollno=$_POST['rno'];

$con=mysqli_connect("localhost","root","","login");

$query="SELECT * FROM `studinfo` WHERE `rollno`=$rollno";

$res=mysqli_query($con,$query);

$result=mysqli_fetch_array($res);

?>

<html>

<head>

<title>Update Form</title>

</head>

<body>

<div id="Logfrm">

<form action="updated.php" method="POST">

<p><label>Roll No: </label>

<input type="text" name="rollno" id="rollno" value=<?php echo $result['rollno']?> />

</p>

<p><label>Student Name: </label>

<input type="text" name="studname" id="studname"value =<?php echo $result['studname']?> />

</p>

<p><label>Class: </label>

<input type="text" name="class" id="class" value=<?php echo $result['class']?> />

</p>

<p><label>Permenant Address: </label>

<input type="textarea" name="padd" id="padd" maxlength="200" value = <?php echo $result['padd']?> >

</textarea>

</p>

<p>

<input type="submit" name="update" id="update" Value="Update" />

</p>

</div>

</form>

</body>

</html>

Preforming Select operation on database:

Consider the following php script to retrieve all records. “showdata.php”

<?php

$con=mysqli_connect("localhost","root","","login");

$query="SELECT * FROM `studinfo`";

$res=mysqli_query($con,$query);

while($result=mysqli_fetch_array($res)){

echo $result['rollno']." ".$result['studname']." ".$result['class']." ".$result['padd']."</br>";

}

?>

Consider the following php script to retrieve all records in html table. “showdataintable.php”

<?php

$con=mysqli_connect("localhost","root","","login");

$query="SELECT * FROM `studinfo`";

$res=mysqli_query($con,$query);

?>

<html>

<body>

<table border=1px align="center" cellpadding="10" >

<th colspan="4">Student Details</th>

<tr>

<td> Roll Number</td>

<td> Student Name</td>

<td> Class</td>

<td> Permenant Address</td>

</tr>

<?php while($result=mysqli_fetch_array($res)){ ?>

<tr><td><?php echo $result['rollno']?></td>

<td><?php echo $result['studname']?></td>

<td><?php echo $result['class']?></td>

<td><?php echo $result['padd']?></td>

</tr>

<?php

//echo $result['rollno']." ".$result['studname']." ".$result['class']." ".$result['padd']."</br>";

}

?>

</table>

</body>

</html>

8.5 Executing query:

mysqli_query() function performs a query against a database.

Syntax:

mysqli_query(connection, query, resultmode)

Parameters:

 connection: this parameter is required; it specifies the MySQL connection to use.

 query: this is also compulsory parameter; it specifies the SQL query string.

 resultmode: it is Optional parameter. it Can be one of the following:

o MYSQLI_USE_RESULT (Use this to retrieve large amount of data)

o MYSQLI_STORE_RESULT (This is default)

Example:

mysqli_query($con,$query)

8.6 Join:

MySQL joins:

MySQL join clause combines records from two or more tables in a relational database. It creates a set that can be saved as a table or used as it is. A JOIN is a means for combining fields from two tables (or more) by using values common to each. 

MySQL specifies four types of JOIN: INNER, LEFT OUTER, RIGHT OUTER, and CROSS INNER JOIN:

The MySQL Inner join only displays the matching records from Table A and Table B (Like an Intersect in Mathematics).

Syntax:

SELECT Table1.Column(s), Table2.Column(s)

FROM Table1

INNER JOIN

Table2 ON

Table1.Common_Column = Table2.Common_Column

database: Login

dept emp

deptid deptname empid empname empsalary deptid

Example:

SELECT emp.empid,emp.empname,emp.empsal,dept.deptname FROM emp INNER JOIN dept ON

emp.deptid=dept.deptid

MySQL Left Join or Left outer join:

MySQL Left Join or Left outer join is to return all the records (or rows) from the Left table, and matching rows from the right table.

Syntax:

SELECT Table1.Column(s), Table2.Column(s)

FROM Table1

LEFT JOIN

Table2 ON

Table1.Common_Column = Table2.Common_Column

Example:

SELECT emp.empid,emp.empname,emp.empsal,dept.deptname FROM emp INNER JOIN dept ON

emp.deptid=dept.deptid

Example:

SELECT * FROM emp LEFT JOIN dept ON emp.deptid=dept.deptid

MySQL Right Outer Join:

MySQL Right Outer Join is one of the Join Type, which is useful to return all the existing records (or rows) from the Right table, and matching rows from the left table. All the Unmatched rows from the left table filled with NULL Values.

Syntax:

SELECT Table1.Column(s), Table2.Column(s)

FROM Table1

RIGHT JOIN

Table2 ON

Table1.Common_Column = Table2.Common_Column

Example:

SELECT * FROM emp RIGHT JOIN dept ON emp.deptid=dept.deptid

MySQL Cross Join:

MySQL Cross Join returns the Cartesian product of both the tables. The Cross Join in MySQL does not require any

common column to join two tables. The Cartesian product means Number of Rows present in Table 1 Multiplied by

Number of Rows present in Table 2.

Syntax:

SELECT Table1.Column(s), Table2.Column(s),

FROM Table1

CROSS JOIN

Table2

Example:

SELECT * FROM `studinfo` CROSS JOIN `user`

No comments:

Post a Comment