SULTHAN's - PHP MySQL programs
- 1. PHP & MySQL
Lab Manual
SSGS DEGREE COLLEGE
Prepared
By
D. SULTHAN BASHA
Lecturer in Computer Science
- 2. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 1
1........
<html>
<head> <title> This is php program to initialize variables</title>
</head>
<Body bgcolor = "green">
<h2>
<?php
$name = "sulthan";
$age = 29;
print "Your Name: $name.<BR>";
print " Your Age: $age.<BR>";
?>
</h2>
</body>
</html>
- 3. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 2
2.........
<html>
<body bgcolor="sky blue">
<h3>
<form method="post">
How many numbers you want?
<input type="number" name="number"/><br>
<input type="submit" value="print"/>
</form>
<?php
$num=$_POST['number'];
for($i=1;$i<=$num;$i++)
{
echo "$i ";
}
?>
</h3>769
287/
</body>
</html>
- 4. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 3
3.......
<html>
<body>
<form method="post">
Enter first number:
<input type = "number" name = "number1"/><br><br>
Enter second number:
<input type = "number" name = "number2"/><br><br>
<input type = "submit" name = "submit" value="Add"/>
</form>
<?php
if(isset($_POST['submit']))
{
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$sum = $number1+$number2;
- 5. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 4
echo "the sum of $number1 and $number2 is $sum";
}
?>
</body>
</html>
4.........
<?php
$num = 25;
if($num%2==0)
{
echo "$num is Even number";
}
else{
echo "$num is Odd number";
}
?>
- 6. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 5
5.......
<?php
$num = 25;
if($num<100)
{
echo "$num is Less than 100";
}
?>
6.......
<?php
$i=10;
while($i>=1)
{
echo "$i<br>";
$i--;
}
?>
- 7. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 6
7.........
<html>
<head>
<style>
.error{color:#FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr=$emailErr=$genderErr=$websiteErr="";
$name=$email=$gender=$comment=$website="";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(empty($_POST["name"])){
$nameErr="Name is required";
}
else
{
$name=test_input($_POST["name"]);
// check if name only contains letters and whitespace
if(!preg_match("/^[a-zA-Z]*$/",$name)) {
$nameErr="Only letters and white space allowed";
- 8. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 7
}
}
if(empty($_POST["email"])) {
$emailErr="Email is required";
} else {
$email=test_input($_POST["email"]);
// check if e-mail address is well-formed
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr="Invalid email format";
}
}
if(empty($_POST["website"])) {
$website="";
} else {
$website=test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if(!preg_match("/b(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-
9+&@#/%=~_|]/i",$website)) {
$websiteErr ="Invalid URL";
}
}
if(empty($_POST["comment"])) {
$comment="";
- 9. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 8
} else {
$comment=test_input($_POST["comment"]);
}
if(empty($_POST["gender"])) {
$genderErr= "Gender is required";
} else {
$gender=test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name:<input type="text" name ="name" value="<?php echo $name;?>">
<span class="error">*<?php echo $nameErr;?></span>
<br><br>
E-mail:<input type="text" name="email" value="<?php echo $email;?>">
- 10. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 9
<span class="error">*<?php echo $emailErr;?></span>
<br><br>
Website:<input type="text" name="website" value="<?php echo $website;?>">
<span class="error">*<?php echo $websiteErr;?></span>
<br><br>
Comment:<textarea name="comment" rows="5" cols="40"><?php echo
$comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender"<?php if(isset($gender) && $gender=="female") echo
"checked";?>
value="female">Female
<input type="radio" name="gender"<?php if(isset($gender) && $gender=="male") echo
"checkede";?>
value="male">Male
<input type="radio" name="gender" <?php if(isset($gender) && $gender=="other") echo
"checked";?>
value="other">Other
<span class="error">*<?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo"<h2>Your Input:</h2>";
echo "$name";
echo "<br>";
- 11. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 10
echo "$email";
echo "<br>";
echo "$website";
echo "<br>";
echo "$comment";
echo "<br>";
echo "$gender";
?>
</body>
</html>
- 12. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 11
8..........
<html>
<head>
<?php
$servername = "localhost:3306";
$username ="avinash";
$password = "avinash";
$dbname = "avinash";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if($conn->connect_error) {
die("Connection failed:". $conn->connect_error);
}
$sql = "SELECT productid, productname, price,MFD FROM product";
$result = $conn->query($sql);
if($result->num_rows>0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<br>".$row["productid"].$row["productname"].$row["price"].$row["MFD"]."<br >";
}
- 13. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 12
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
- 14. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 13
9..........
<?php
$servername="localhost:3306";
$dbname="avinash";
$username="avinash";
$password="avinash";
//Create connection
$conn=mysqli_connect($servername,$dbname, $username,$password);
//Check connection
if(!$conn)
{
die("Connection failed:".mysql_error());
}
echo "connected successfully";
$sql = "SELECT * FROM `product`";
$result=mysqli_query($conn,$sql);
if($result)
- 15. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 14
{
$num_of_rows=mysqli_num_rows($result);
echo "<br>ProductID ProductName Price MFD"."<br>";
while($row = $result->fetch_assoc())
{
// echo"<br>ProductID:".$row["productid"]."ProductName: ".$row["productname"]."Price:
".$row//["price"]."MFD: ".$row["MFD"]."<br>";
printf("%d",$row["productid"]);
printf("%s",$row["productname"]);
printf("%d",$row["price"]);
printf("%s",$row["MFD"]);
echo ".<br>";
}
printf("<br>Result Set %d rows n", $num_of_rows);
}
else{
printf("Could Not Retrieve", mysqli_error($mysqli));
}
mysqli_free_result($result);
mysqli_close($conn);
?>
- 16. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 15
10........
<!DOCTYPE html>
<html>
<body>
<?php
echo "The time is".date("h:i:sa");
?>
</body>
</html>
11............
<?php
$name="AVINASH";
$age="19";
print "your name: $name.<BR>";
print "your age: $age.<BR>";
?>
- 17. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 16
12......
<?php
for($i=1;$i<=10;$i++)
{
echo"$i<br>";
}
?>
- 18. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 17
13.........
<html>
<body>
<form method="post">
<input type="text" name="text"/><br>
<input type="submit" Value="submit"/><br>
</form>
<?php
$str=$_POST['text'];
if($str==strrev($str))
{
echo "$str is a palindrome";
}
else
{
echo "$str is not a palindrome";
}
?>
</body>
</html>
- 19. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 18
14..........
<html>
<body>
<?php
echo strrev("Hello WQorld");
?>
</body>
</html>
- 20. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 19
15...........
<html>
<body>
<?php
$favcolor = "blue";
switch ($favcolor)
{
case "red":
echo "Your favorite color is red !";
break;
case "blue":
echo "Your favorite color is blue !";
break;
case "green":
echo "Your favorite color is green !";
break;
default;
echo"your favorite color is neither red,blue,nor green!";
}
?>
</body>
</html>
- 21. PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 20
16........
<html>
<body>
<?php
$t=date("h:m:s,d:m:y");
echo"<P>The hour(of the server) is ".$t;
echo"and will give the following message:</p>";
if($t<"10")
{
echo"Have a good morning!";
}
elseif($t<"20")
{
echo "Have a good day!";
}
else
{
echo"Have a good night!";
}
?>
</body>
</html>