SlideShare a Scribd company logo
PRESENTATION ON
PHP WITH MYSQL DATABASE
Prepared By
Ms. R. Gomathijayam
Assistant Professor
Bon Secours College for Women
Thanjavur
PHP INTRODUCTION
What is PHP?
• PHP is a server side programming language, and a
powerful tool for making dynamic and interactive Web
pages.
• PHP is an acronym for "PHP: Hypertext Preprocessor“
• Original Name: Personal Home Page
• PHP is created by Rasmus Lerdorf in 1994.
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server
•PHP 7 is the latest stable release.
What Can PHP Do?
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and close files
on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data
PHP Supported Web Server
• Microsoft Internet Information Server
• Apache Server
• Xitami
• Sambar Server
PHP Supported Editors
• Macintosh’s BBEdit
• Simple Text
• Windows Notepad or Wordpad
• Macromedia Dream Weaver
What is a PHP File?
• PHP files can contain text, HTML, CSS, JavaScript, and
PHP code. It is executed on the server, and the result is
returned to the browser as plain HTML.
• PHP files have extension ".php“
• Download it from the official PHP resource: www.php.net
• PHP runs on various platforms (Windows, Linux, Unix,
Mac OS X, etc.)
Basic PHP Syntax
• A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
PHP Case Sensitivity
• In PHP, No keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are case-
sensitive.
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
PHP Form Handling
• The PHP super globals $_GET and $_POST are used to
collect form-data.
Example
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
Getting form data into PHP pages
• To display the submitted data you could simply echo all
the variables. The "welcome.php“
<html>
<body>
Welcome
<?php echo $_POST["name"]; ?>
<br>
Your email address is:
<?php
echo $_POST["email"];
?>
</body>
</html>
Using the HTTP GET method
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit” value=“send”>
</form>
</body>
</html>
PHP COOKIES
What is a Cookie?
• A cookie is a small file that the server embeds on the
user's computer. Each time the same computer requests a
page with a browser, it will send the cookie too.
• With PHP, we can both create and retrieve cookie values.
Syntax
setcookie(name, value, expire, path, domain, secure);
• Only the name parameter is required. All other parameters
are optional.
Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not
set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
PHP Sessions
• A session is a way to store information (in variables) to be
used across multiple pages.
• Session variables hold information about one single user,
and are available to all pages in one application.
Start a PHP Session
•A session is started with the session_start() function.
• Session variables are set with the PHP global variable:
$_SESSION.
Example
<?php
// Start the session
session_start();
?>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
PHP - What is OOP?
• From PHP5, we can also write PHP code in an object-
oriented style. bject-Oriented programming is faster and
easier to execute.
• A class is a template for objects, and an object is an
instance of class.
Example
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
PHP with MySQL
• With PHP, you can connect to and manipulate databases.
MySQL is the most popular database system used with
PHP.
What is MySQL?
• MySQL is a database system used on the web and
runs on a server.
• MySQL uses standard SQL.
• MySQL compiles on a number of platforms.
• MySQL is developed, distributed, and supported by
Oracle Corporation.
Connect to PHP with MySQL
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username,
$password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Create a MySQL Database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE)
{
echo "Database created successfully";
}
else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
Table Creation
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY
KEY, firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT
CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
Insert Database
$sql = "INSERT INTO MyGuests (firstname, lastname,
email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Data Retrieval
$sql = "SELECT id, firstname, lastname FROM
MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " .
$row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
Deletion
$sql = "DELETE FROM MyGuests WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
}
else {
echo "Error deleting record: " . $conn->error;
}
Data Updation
$sql = "UPDATE MyGuests SET lastname='Doe'
WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
}
else {
echo "Error updating record: " . $conn->error;
}
Thank you
Ad

More Related Content

What's hot (20)

REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
Halil Burak Cetinkaya
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
Crud tutorial en
Crud tutorial enCrud tutorial en
Crud tutorial en
forkgrown
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Java Collections
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode
 
Php sessions
Php sessionsPhp sessions
Php sessions
JIGAR MAKHIJA
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
TheCreativedev Blog
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Igor Anishchenko
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
Mudasir Syed
 
PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP ServerPHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
Rajiv Bhatia
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
Kandarp Tiwari
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
Express js
Express jsExpress js
Express js
Manav Prasad
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
Intro to React
Intro to ReactIntro to React
Intro to React
Justin Reock
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
Reem Alattas
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
Arnold Asllani
 

Similar to Php with mysql ppt (20)

PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
adeel990
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
Sorabh Jain
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
souridatta
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
HambardeAtharva
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
Guni Sonow
 
Php
PhpPhp
Php
Shagufta shaheen
 
PHP language presentation
PHP language presentationPHP language presentation
PHP language presentation
Annujj Agrawaal
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
husnara mohammad
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
Dr. Ramkumar Lakshminarayanan
 
Php talk
Php talkPhp talk
Php talk
Jamil Ramsey
 
PHP.pptx is the Best Explanation of ppts
PHP.pptx is the Best Explanation of pptsPHP.pptx is the Best Explanation of ppts
PHP.pptx is the Best Explanation of ppts
AkhileshPansare
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
rICh morrow
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
musrath mohammad
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
Mandakini Kumari
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
Tom Praison Praison
 
php (Hypertext Preprocessor)
php (Hypertext Preprocessor)php (Hypertext Preprocessor)
php (Hypertext Preprocessor)
Chandan Das
 
PHP
PHPPHP
PHP
Chandan Das
 
Prersentation
PrersentationPrersentation
Prersentation
Ashwin Deora
 
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
Unit 5-PHP  Declaring variables, data types, array, string, operators, Expres...Unit 5-PHP  Declaring variables, data types, array, string, operators, Expres...
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
DRambabu3
 
Ad

More from Rajamanickam Gomathijayam (9)

Computer Application in Business
Computer Application in BusinessComputer Application in Business
Computer Application in Business
Rajamanickam Gomathijayam
 
Sololearn cert
Sololearn certSololearn cert
Sololearn cert
Rajamanickam Gomathijayam
 
Uml ppt
Uml pptUml ppt
Uml ppt
Rajamanickam Gomathijayam
 
Coding for php with mysql
Coding for php with mysqlCoding for php with mysql
Coding for php with mysql
Rajamanickam Gomathijayam
 
Corel Draw Introduction
Corel Draw IntroductionCorel Draw Introduction
Corel Draw Introduction
Rajamanickam Gomathijayam
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
Rajamanickam Gomathijayam
 
Introducction to Algorithm
Introducction to AlgorithmIntroducction to Algorithm
Introducction to Algorithm
Rajamanickam Gomathijayam
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
Rajamanickam Gomathijayam
 
Network Concepts
Network ConceptsNetwork Concepts
Network Concepts
Rajamanickam Gomathijayam
 
Ad

Recently uploaded (20)

Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 

Php with mysql ppt

  • 1. PRESENTATION ON PHP WITH MYSQL DATABASE Prepared By Ms. R. Gomathijayam Assistant Professor Bon Secours College for Women Thanjavur
  • 2. PHP INTRODUCTION What is PHP? • PHP is a server side programming language, and a powerful tool for making dynamic and interactive Web pages. • PHP is an acronym for "PHP: Hypertext Preprocessor“ • Original Name: Personal Home Page • PHP is created by Rasmus Lerdorf in 1994. • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server •PHP 7 is the latest stable release.
  • 3. What Can PHP Do? • PHP can generate dynamic page content • PHP can create, open, read, write, delete, and close files on the server • PHP can collect form data • PHP can send and receive cookies • PHP can add, delete, modify data in your database • PHP can be used to control user-access • PHP can encrypt data
  • 4. PHP Supported Web Server • Microsoft Internet Information Server • Apache Server • Xitami • Sambar Server PHP Supported Editors • Macintosh’s BBEdit • Simple Text • Windows Notepad or Wordpad • Macromedia Dream Weaver
  • 5. What is a PHP File? • PHP files can contain text, HTML, CSS, JavaScript, and PHP code. It is executed on the server, and the result is returned to the browser as plain HTML. • PHP files have extension ".php“ • Download it from the official PHP resource: www.php.net • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • 6. Basic PHP Syntax • A PHP script starts with <?php and ends with ?>: <?php // PHP code goes here ?> Example <!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?>
  • 7. PHP Case Sensitivity • In PHP, No keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are case- sensitive. <!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
  • 8. PHP Form Handling • The PHP super globals $_GET and $_POST are used to collect form-data. Example <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
  • 9. Getting form data into PHP pages • To display the submitted data you could simply echo all the variables. The "welcome.php“ <html> <body> Welcome <?php echo $_POST["name"]; ?> <br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>
  • 10. Using the HTTP GET method <html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit” value=“send”> </form> </body> </html>
  • 11. PHP COOKIES What is a Cookie? • A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. • With PHP, we can both create and retrieve cookie values. Syntax setcookie(name, value, expire, path, domain, secure); • Only the name parameter is required. All other parameters are optional.
  • 12. Example <?php $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?>
  • 13. PHP Sessions • A session is a way to store information (in variables) to be used across multiple pages. • Session variables hold information about one single user, and are available to all pages in one application. Start a PHP Session •A session is started with the session_start() function. • Session variables are set with the PHP global variable: $_SESSION.
  • 14. Example <?php // Start the session session_start(); ?> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?>
  • 15. PHP - What is OOP? • From PHP5, we can also write PHP code in an object- oriented style. bject-Oriented programming is faster and easier to execute. • A class is a template for objects, and an object is an instance of class. Example <?php class Fruit { // Properties public $name; public $color;
  • 16. // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; }} $apple = new Fruit(); $banana = new Fruit(); $apple->set_name('Apple'); $banana->set_name('Banana'); echo $apple->get_name(); echo "<br>"; echo $banana->get_name(); ?>
  • 17. PHP with MySQL • With PHP, you can connect to and manipulate databases. MySQL is the most popular database system used with PHP. What is MySQL? • MySQL is a database system used on the web and runs on a server. • MySQL uses standard SQL. • MySQL compiles on a number of platforms. • MySQL is developed, distributed, and supported by Oracle Corporation.
  • 18. Connect to PHP with MySQL <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>
  • 19. Create a MySQL Database $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; } $conn->close();
  • 20. Table Creation $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "Error creating table: " . $conn->error; }
  • 21. Insert Database $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; }
  • 22. Data Retrieval $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; }
  • 23. Deletion $sql = "DELETE FROM MyGuests WHERE id=3"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; }
  • 24. Data Updation $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; }