PHP
Practical :- 1
AIM :- Write a PHP script to create a database StudentDB.
<?php
$connect = mysqli_connect('localhost','root','');
$query = "CREATE DATABASE StudentDB";
$result = mysqli_query($connect,$query);
?>
OUTPUT :-
34
Practical :- 2
AIM :- Write a PHP script to list all the databases available in mysql.
<?php
$connect=mysqli_connect('localhost','root','');
$query="SHOW databases";
$result=mysqli_query($connect,$query);
while($i=mysqli_fetch_array($result))
print($i[0]."<br>");
?>
OUTPUT :-
35
Practical :- 3
AIM :- Write a PHP script to list all the tables available in a particular database.
<?php
$connect=mysqli_connect('localhost','root','','register');
$query="SHOW tables";
$result=mysqli_query($connect,$query);
while($i=mysqli_fetch_array($result))
print($i[0]."<br>");
?>
OUTPUT :-
36
Practical :- 4
AIM :- Write a PHP script to create a table student in the database StudentDB.
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="CREATE TABLE students(enroll_no int(10) primary key,fname varchar(20),lname
varchar(20),branch varchar(20),contact int(10))";
$result=mysqli_query($connect,$query);
print "<script> alert('create table') </script>";
?>
OUTPUT :-
37
Practical :- 5
AIM :- Write a PHP script to insert a row into the table student. The values to
be inserted are taken from a HTML page.
Form html code:
<html>
<head>
<title>STUDENT</title>
</head>
<body>
<h2>STUDENT</h2>
<form action="[Link]" method="post">
<fieldset>
<label>Enroll_No</label><br>
<input type="text" name="enroll_no" required><br>
<label>Full name</label><br>
<input type="text" name="fname" required><br>
<label>Last name</label><br>
<input type="text" name="lname" required><br>
<label>Branch</label><br>
<select name="branch">
<option value="Computer">Computer</option>
<option value="Mech">Mechanical</option>
38
<option value="EC">EC</option>
</select><br>
<label>Contact_No</label><br>
<input type="tel" name="contact" pattern="[0-9]{10}" maxlength="10"
required><br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</fieldset>
</form>
</body>
</html>
Php code :
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
if($_SERVER['REQUEST_METHOD']=='POST')
$s1=$_POST['enroll_no'];
$s2=$_POST['fname'];
$s3=$_POST['lname'];
$s4=$_POST['branch'];
$s5=$_POST['contact'];
if(!empty($s1))
39
{
$insert="insert into students values('$s1','$s2','$s3','$s4','$s5')";
$result=mysqli_query($connect,$insert);
echo "<script type='text/javascript'> alert('success') </script>";
else{
echo "<script type='text/javascript'> alert('fail') </script>";
?>
OUTPUT :-
Practical :- 6
40
AIM :- Write a PHP script to alter student table. For ex: modify sname by
increasing its length.
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="ALTER TABLE students MODIFY COLUMN fname varchar(40)";
$result=mysqli_query($connect,$query);
echo "Update successfully.";
?>
OUTPUT :-
Practical :- 7
41
AIM :- Write a PHP script to list all the records in the student table in tabular
format.
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="SELECT * from students";
$result=mysqli_query($connect,$query);
echo "<table border=1>";
echo
"<tr><th>enroll_no</th><th>fname</th><th>lname</th><th>branch</th><th>
contact</th></tr>";
while($i=mysqli_fetch_arraY($result))
echo "<tr>";
echo "<td>".$i['enroll_no']."</td>";
echo "<td>".$i['fname']."</td>";
echo "<td>".$i['lname']."</td>";
echo "<td>".$i['branch']."</td>";
echo "<td>".$i['contact']."</td>";
echo "</tr>";
echo "</table>";
?>
42
OUTPUT :-
Practical :- 8
43
AIM :- Write a PHP script to delete all rows from student table whose roll
numbers are between 1 and 3.
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="DELETE from students WHERE enroll_no between 1 and 3 ";
$result=mysqli_query($connect,$query);
echo "Records deleted successfully";
?>
OUTPUT :-
44
Practical :- 9
AIM :- Write a PHP script to drop the table student and drop the database
StudentDB.
Drop table:
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="DROP TABLE students";
$result=mysqli_query($connect,$query);
echo "Table deleted successfully";
?>
OUTPUT :-
45
Drop database :
<?php
$connect=mysqli_connect('localhost','root','','studentdb');
$query="DROP DATABASE studentdb";
$result=mysqli_query($connect,$query);
echo "Database deleted successfully";
?>
OUTPUT :-
46