Unit IV Chapter 8 - Database Connectivity With MySQL - Copy
Unit IV Chapter 8 - Database Connectivity With MySQL - Copy
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
<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 :     ";
?>
<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>";
?>
<?php
$con=mysqli_connect("localhost","root","","login");
$res=mysqli_query($con,$query);
?>
<html>
<body>
<select name="rno">
<?php
$i=0;
while($result=mysqli_fetch_array($res)){
?>
<?php
?>
</select>
</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");
$res=mysqli_query($con,$query);
$result=mysqli_fetch_array($res);
?>
<html>
<head>
<title>Update Form</title>
</head>
<body>
<div id="Logfrm">
</p>
</p>
<p><label>Class: </label>
</p>
<input type="textarea" name="padd" id="padd" maxlength="200" value = <?php echo $result['padd']?> >
</textarea>
</p>
<p>
</p>
</div>
</form>
</body>
</html>
Preforming Select operation on database:
<?php
$con=mysqli_connect("localhost","root","","login");
$res=mysqli_query($con,$query);
while($result=mysqli_fetch_array($res)){
?>
Consider the following php script to retrieve all records in html table. “showdataintable.php”
<?php
$con=mysqli_connect("localhost","root","","login");
$res=mysqli_query($con,$query);
?>
<html>
<body>
<tr>
<td> Class</td>
</tr>
</tr>
<?php
?>
</table>
</body>
</html>
Syntax:
Parameters:
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:
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:
FROM Table1
LEFT JOIN
Table2 ON
Table1.Common_Column = Table2.Common_Column
Example:
Example:
SELECT * FROM emp LEFT JOIN dept ON emp.deptid=dept.deptid
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:
FROM Table1
RIGHT JOIN
Table2 ON
Table1.Common_Column = Table2.Common_Column
Example:
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:
FROM Table1
CROSS JOIN
Table2
Example: