SlideShare une entreprise Scribd logo
1  sur  21
PHP & MySQL
Lab Manual
SSGS DEGREE COLLEGE
Prepared
By
D. SULTHAN BASHA
Lecturer in Computer Science
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>
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>
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;
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";
}
?>
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--;
}
?>
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";
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="";
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;?>">
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>";
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>
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 >";
}
PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 12
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
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)
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);
?>
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>";
?>
PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 16
12......
<?php
for($i=1;$i<=10;$i++)
{
echo"$i<br>";
}
?>
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>
PHP & MySQL
B.Sc(C.S) – Cluster [C2] Page 18
14..........
<html>
<body>
<?php
echo strrev("Hello WQorld");
?>
</body>
</html>
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>
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>

Contenu connexe

Tendances

PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1Kanchilug
 
Date difference[1]
Date difference[1]Date difference[1]
Date difference[1]shafiullas
 
PHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post MethodsPHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post MethodsAl-Mamun Sarkar
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkDirk Haun
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in phpherat university
 
Database Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlDatabase Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlAl-Mamun Sarkar
 
PHP記帳網頁教材(第一頁是空白的)
PHP記帳網頁教材(第一頁是空白的)PHP記帳網頁教材(第一頁是空白的)
PHP記帳網頁教材(第一頁是空白的)TaiShunHuang
 
Gem christmas calendar
Gem christmas calendarGem christmas calendar
Gem christmas calendarerichsen
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample PhpJH Lee
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnSandro Zaccarini
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !Matheus Marabesi
 
Componentization css angular
Componentization css angularComponentization css angular
Componentization css angularDavid Amend
 
Pemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan SessionPemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan SessionNur Fadli Utomo
 
Php web backdoor obfuscation
Php web backdoor obfuscationPhp web backdoor obfuscation
Php web backdoor obfuscationSandro Zaccarini
 

Tendances (19)

TICT #13
TICT #13TICT #13
TICT #13
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1
 
Date difference[1]
Date difference[1]Date difference[1]
Date difference[1]
 
PHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post MethodsPHP Lecture 4 - Working with form, GET and Post Methods
PHP Lecture 4 - Working with form, GET and Post Methods
 
Yql && Raphaël
Yql && RaphaëlYql && Raphaël
Yql && Raphaël
 
Ubi comp27nov04
Ubi comp27nov04Ubi comp27nov04
Ubi comp27nov04
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application Framework
 
Insertcustomer
InsertcustomerInsertcustomer
Insertcustomer
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in php
 
Database Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlDatabase Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and Mysql
 
PHP記帳網頁教材(第一頁是空白的)
PHP記帳網頁教材(第一頁是空白的)PHP記帳網頁教材(第一頁是空白的)
PHP記帳網頁教材(第一頁是空白的)
 
Gem christmas calendar
Gem christmas calendarGem christmas calendar
Gem christmas calendar
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample Php
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vuln
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !
 
Javascript
JavascriptJavascript
Javascript
 
Componentization css angular
Componentization css angularComponentization css angular
Componentization css angular
 
Pemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan SessionPemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan Session
 
Php web backdoor obfuscation
Php web backdoor obfuscationPhp web backdoor obfuscation
Php web backdoor obfuscation
 

Similaire à SULTHAN's - PHP MySQL programs

Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivityMouli Chandira
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
PHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxPHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxCynthiaKendi1
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011John Ford
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kianphelios
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 

Similaire à SULTHAN's - PHP MySQL programs (20)

PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
PHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptxPHP DATABASE MANAGEMENT.pptx
PHP DATABASE MANAGEMENT.pptx
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 

Plus de SULTHAN BASHA

Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSULTHAN BASHA
 
Sulthan's DBMS for_Computer_Science
Sulthan's DBMS for_Computer_ScienceSulthan's DBMS for_Computer_Science
Sulthan's DBMS for_Computer_ScienceSULTHAN BASHA
 
SULTHAN's PHP, MySQL & wordpress
SULTHAN's PHP, MySQL & wordpressSULTHAN's PHP, MySQL & wordpress
SULTHAN's PHP, MySQL & wordpressSULTHAN BASHA
 
SULTHAN's ICT-2 for UG courses
SULTHAN's ICT-2 for UG coursesSULTHAN's ICT-2 for UG courses
SULTHAN's ICT-2 for UG coursesSULTHAN BASHA
 
SULTHAN's - Data Structures
SULTHAN's - Data StructuresSULTHAN's - Data Structures
SULTHAN's - Data StructuresSULTHAN BASHA
 
SULTHAN's - ICT-1 for U.G courses in India
SULTHAN's - ICT-1 for U.G courses in IndiaSULTHAN's - ICT-1 for U.G courses in India
SULTHAN's - ICT-1 for U.G courses in IndiaSULTHAN BASHA
 
SULTHAN's - C Programming Language notes
SULTHAN's - C Programming Language notesSULTHAN's - C Programming Language notes
SULTHAN's - C Programming Language notesSULTHAN BASHA
 

Plus de SULTHAN BASHA (7)

Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
 
Sulthan's DBMS for_Computer_Science
Sulthan's DBMS for_Computer_ScienceSulthan's DBMS for_Computer_Science
Sulthan's DBMS for_Computer_Science
 
SULTHAN's PHP, MySQL & wordpress
SULTHAN's PHP, MySQL & wordpressSULTHAN's PHP, MySQL & wordpress
SULTHAN's PHP, MySQL & wordpress
 
SULTHAN's ICT-2 for UG courses
SULTHAN's ICT-2 for UG coursesSULTHAN's ICT-2 for UG courses
SULTHAN's ICT-2 for UG courses
 
SULTHAN's - Data Structures
SULTHAN's - Data StructuresSULTHAN's - Data Structures
SULTHAN's - Data Structures
 
SULTHAN's - ICT-1 for U.G courses in India
SULTHAN's - ICT-1 for U.G courses in IndiaSULTHAN's - ICT-1 for U.G courses in India
SULTHAN's - ICT-1 for U.G courses in India
 
SULTHAN's - C Programming Language notes
SULTHAN's - C Programming Language notesSULTHAN's - C Programming Language notes
SULTHAN's - C Programming Language notes
 

Dernier

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Dernier (20)

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

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>