SlideShare a Scribd company logo
PHP AND MYSQL
CONTENTS
• Introduction to PHP and Mysql
• XAMPP server
• Basic PHP program
• MySql in XAMPP
• Login and User registration system.
INTRODUCTION TO PHP AND MYSQL
• The term PHP is an acronym for – Hypertext Preprocessor. PHP is a server-side scripting language
designed specifically for web development also known as Personal Home Page.
• It is open-source which means it is free to download and use. It is very simple to learn and use.
The file extension of PHP is “.php”.
• PHP can perform various tasks, including handling form data, generating dynamic page content,
managing databases, and interacting with servers.
• Features of PHP
• DynamicTyping: PHP is dynamically typed, meaning you don’t need to declare the data type of a variable
explicitly.
• Cross-Platform: PHP runs on various platforms, making it compatible with different operating systems.
• Database Integration: PHP provides built-in support for interacting with databases, such as MySQL,
PostgreSQL, and others.
• Server-Side Scripting: PHP scripts are executed on the server, generating HTML that is sent to the
client’s browser.
• A PHP script can be placed anywhere in the document.A PHP script starts
with <?php and ends with ?>
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
• Any variables declared in PHP must begin with a dollar sign ($), followed by the variable name.
• A variable can have long descriptive names (like $factorial, $even_nos) or short names (like $n or
$f or $x)
• A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9,
and ‘_’) in their name. Even it cannot start with a number.
• In PHP, both echo and print are language constructs used to output strings. echo can output
multiple strings separated by commas without parentheses, while `print` can only output one
string and always returns 1.
• PHP is insensitive to whitespace. PHP is case-sensitive. All the keywords, functions, and
class names in PHP (while, if, echo, else, etc) are NOT case-sensitive except variables.
• PHP allows eight different types of data types. All of them are discussed below. There are pre-
defined, user-defined, and special data types.
• The predefined data types are:
• Boolean
• Integer
• Double
• String
• The user-defined (compound) data types are:
• Array
• Objects
• The special data types are:
• NULL
• Resource
<?php
// These are all valid declarations
$val = 5;
$val2 = 2.4;
$val3 = 200.23;
$x_Y = "gfg";
$_X = "GeeksforGeeks";
// This is an invalid declaration as it
// begins with a number
$10_ val = 56;
// This is also invalid as it contains
// special character other than _
$f.d = "num";
?>
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("red", "Volvo");
var_dump($myCar);
• Constants are either identifiers or simple names that can be assigned any fixed values.They are
similar to a variable except that they can never be changed.
• The define() function in PHP is used to create a constant as shown below:
• define(name, value, case_insensitive)
• The const keyword is used to declare a class constant. A constant is unchangeable once it is
declared.The constant class declared inside the class definition.
• You can also create constant variable using const keyword.
const MYCAR = "Volvo";
• const vs. define()
• const are always case-sensitive
• define() has has a case-insensitive option.
• const cannot be created inside another block scope, like inside a function or inside an if
statement.
• define can be created inside another block scope.
• PHP provides us with four conditional statements:
• if statement
• if…else statement
• if…elseif…else statement
• switch statement
• In PHP break is used to immediately terminate the loop and the program control resumes at the
next statement following the loop.
• The continue statement is used within a loop structure to skip the loop iteration and continue
execution at the beginning of condition execution. It is mainly used to skip the current iteration
and check for the next condition.
• The continue accepts an optional numeric value that tells how many loops you want to skip. Its
default value is 1.
• PHP supports four types of looping techniques;
1. for loop
2. while loop
3. do-while loop
4. foreach loop
• A function is a block of code written in a program to perform some specific task.A function name always
begins with the keyword function.
• Built-in functions : PHP provides us with huge collection of built-in library functions.These functions are
already coded and stored in form of functions.To use those we just need to call them as per our
requirement like, var_dump, fopen(), print_r(), gettype() and so on.
• User Defined Functions :Apart from the built-in functions, PHP allows us to create our own
customised functions called the user-defined functions.
• Arrow functions, also known as “short closures”, is a new feature introduced in PHP 7.4 that provides a
more concise syntax for defining anonymous functions.Arrow functions allow you to define a function in a
single line of code, making your code more readable and easier to maintain.
PHP OOP’S CONCEPT
• Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.
• Like C++ and Java, PHP also supports object oriented programming concepts like Classes and
Objects, Encapsulation, Inheritance,Abstraction, Polymorphism, Interfaces.
• Classes and Objects :
1.Classes are the blueprints of objects. One of the big differences between functions and classes is
that a class contains both data (variables) and functions that form a package called an:‘object’.
2.Class is a programmer-defined data type, which includes local methods and local variables.
3.Class is a collection of objects. Object has properties and behavior.
• Constructors are the blueprints for object creation providing values for member functions and member
variables.
• Once the object is initialized, the constructor is automatically called. Destructors are for destroying
objects and automatically called at the end of execution.
• The constructor is defined in the public section of the Class. Even the values to properties of the class are
set by Constructors.
• Constructor types:
• Default Constructor: It has no parameters, but the values to the default constructor can be passed
dynamically.
• Parameterized Constructor: It takes the parameters, and also you can pass different values to the
data members.The -> operator is used to set value for the variables. In the constructor method, you can
assign values to the variables during object creation.
• Copy Constructor: It accepts the address of the other objects as a parameter.
• Destructor is also a special member function which is exactly the reverse of constructor method and is
called when an instance of the class is deleted from the memory. Destructors (__destruct ( void): void) are
methods which are called when there is no reference to any object of the class or goes out of scope or
about to release explicitly.
They don’t have any types or return value. It is just called before de-allocating memory for an object or during the finish of
execution of PHP scripts or as soon as the execution control leaves the block.
<?php
class SomeClass
{
function __construct()
{
echo "In constructor, ";
$this->name = "Class object! ";
}
function __destruct()
{
echo "destroying " . $this->name . "n";
}
}
$obj = new Someclass();
?>
• Inheritance :
• The child class will inherit all the public and protected properties and methods from the parent class. In
addition, it can have its own properties and methods.
• An inherited class is defined by using the extend keyword
• Inherited methods can be overridden by redefining the methods (use the same name) in the child class.
• The final keyword can be used to prevent class inheritance or to prevent method overriding.
• PHP only supports single inheritance: a child class can inherit only from one single parent.
• So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
• Traits are used to declare methods that can be used in multiple classes.Traits can have methods and
abstract methods that can be used in multiple classes, and the methods can have any access modifier
(public, private, or protected).
• Traits are declared with them trait keyword
• In the PHP each and every property of a class in must have one of three visibility levels, known
as public, private, and protected.
• Public: Public properties can be accessed by any code, whether that code is inside or outside the class. If a
property is declared public, its value can be read or changed from anywhere in your script.
• Private: Private properties of a class can be accessed only by code inside the class. So if we create a
property that’s declared private, only methods and objects inside the same class can access its contents.
• Protected: Protected class properties are a bit like private properties in that they can’t be accessed by
code outside the class, but there’s one little difference in any class that inherits from the class i.e. base class
can also access the properties.
Class
Member
Access
Specifier
Access from own
class
Accessible from
derived class
Accessible by
Object
Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes
• Abstract classes are the classes in which at least one method is abstract. Unlike C++ abstract classes in
PHP are declared with the help of abstract keyword.
• Use of abstract classes are that all base classes implementing this class should give implementation of
abstract methods declared in parent class. An abstract class can contain abstract as well as non abstract
methods.
• Like Java, PHP also an instance of abstract class can not be created.
• Like C++ or Java abstract class in PHP can contain constructor also.
• An Interface is defined just like a class is defined but with the class keyword replaced by the interface
keyword and just the function prototypes.The interface contains no data variables.The interface is helpful in
a way that it ensures to maintain a sort of metadata for all the methods a programmer wishes to work on.
• Few characteristics of an Interface are:
• An interface consists of methods that have no implementations, which means the interface methods are
abstract methods.
• All the methods in interfaces must have public visibility scope.
• Interfaces are different from classes as the class can inherit from one class only whereas the class can
implement one or more interfaces.
• Concrete Class:The class which implements an interface is called the Concrete Class. It must implement all
the methods defined in an interface. Interfaces of the same name can’t be implemented because of ambiguity
error. Just like any class, an interface can be extended using the extends operator
• Namespaces are qualifiers that solve two different problems:
1. They allow for better organization by grouping classes that work together to perform a task
2. They allow the same name to be used for more than one class
• For example, you may have a set of classes which describe an HTML table, such as Table, Row and Cell while
also having another set of classes to describe furniture, such as Table, Chair and Bed. Namespaces can be used
to organize the classes into two different groups while also preventing the two classes Table and Table from
being mixed up.
MYSQL
MySQL is an open-source relational database management system (RDBMS). It is the most popular database
system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation.
• The data in a MySQL database are stored in tables which consists of columns and rows.
• MySQL is a database system that runs on a server.
• MySQL is ideal for both small and large applications.
• MySQL is very fast, reliable, and easy to use database system. It uses standard SQL
• MySQL compiles on a number of platforms.
PHP 5 and later can work with a MySQL database using:
1. MySQLi extension.
2. PDO (PHP Data Objects).
• Difference Between MySQLi and PDO
• PDO works on 12 different database systems, whereas MySQLi works only with MySQL databases.
• Both PDO and MySQLi are object-oriented, but MySQLi also offers a procedural API.
• If at some point of development phase, the user or the development team wants to change the database
then it is easy to that in PDO than MySQLi as PDO supports 12 different database systems. He/She would
have to only change the connection string and a few queries.With MySQLi, he/she will need to rewrite the
entire code including the queries.
• There are three ways of working with MySQl and PHP
1. MySQLi (object-oriented)
2. MySQLi (procedural)
3. PDO
• Using MySQLi object-oriented procedure:We can use the MySQLi object-oriented procedure to establish
a connection to MySQL database from a PHP script.
• Syntax:
<?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";
?>
• Using MySQLi procedural procedure :There is also a procedural approach of MySQLi to establish a
connection to MySQL database from a PHP script as described below.
• Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
• Using PDO procedure: PDO stands for PHP Data Objects.That is, in this method we connect to the
database using data objects in PHP as described below:
• Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
• Using MySQLi object oriented procedure
Syntax
$conn->close();
• Using MySQLi procedural procedure
Syntax
mysqli_close($conn);
• Using PDO procedure
Syntax
$conn = null;
What is a database?
Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of
data from database and organizes the data in the form of tables, views, schemas, reports etc.
The basic steps to create MySQL database using PHP are:
• Establish a connection to MySQL server from your PHP script as described in this article.
• If the connection is successful, write a SQL query to create a database and store it in a string variable.
• Execute the query.
• Using MySQLi Object-oriented procedure: If the MySQL connection is established using Object-
oriented procedure then we can use the query() function of mysqli class to execute our query as
described in the below syntax.
• Using MySQLi Procedural procedure: If the MySQL connection is established using procedural
procedure then we can use the mysqli_query() function of PHP to execute our query as described in
the below syntax.
• Using PDO procedure: If the MySQL connection is established using PDO procedure then we can
execute our query as described in the below syntax.
XAMPP SERVER
The collection of related data is called a database. XAMPP stands for cross-platform,Apache, MySQL, PHP,
and Perl. It is among the simple light-weight local servers for website development.
Requirements: XAMPP web server procedure:
• Start XAMPP server by starting Apache and MySQL.
• Write PHP script for connecting to XAMPP.
• Run it in the local browser.
• Database is successfully created which is based on the PHP code.
• In PHP, we can connect to the database using XAMPP web server by using the following path.
localhost/phpMyAdmin
• Insert
• Select
• Delete
• Where
• Update
• OrderBy
• GroupBy having
• Limit
In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the
result set.The Limit Clause accepts one or two arguments which are offset and count.The value of both
the parameters can be zero or positive integers.
• Offset: It is used to specify the offset of the first row to be returned.
Count: It is used to specify the maximum number of rows to be returned.
• The Limit clause accepts one or two parameters, whenever two parameters are specified, the first is the
offset and the second denotes the count whereas whenever only one parameter is specified, it denotes
the number of rows to be returned from the beginning of the result set.
PHP FORM HANDLING
• GET vs. POST
• Both GET and POST create an array (e.g. array( key1 => value1, key2 =>
value2, key3 => value3, ...)). This array holds key/value pairs, where keys are
the names of the form controls and values are the input data from the user.
• Both GET and POST are treated as $_GET and $_POST. These are superglobals,
which means that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do anything
special.
• $_GET is an array of variables passed to the current script via the URL
parameters.
• $_POST is an array of variables passed to the current script via the HTTP POST
method.
• When to use GET?
• Information sent from a form with the GET method is visible to everyone (all variable names and
values are displayed in the URL). GET also has limits on the amount of information to send. The
limitation is about 2000 characters. However, because the variables are displayed in the URL, it is
possible to bookmark the page. This can be useful in some cases.
• GET may be used for sending non-sensitive data.
• Note: GET should NEVER be used for sending passwords or other sensitive information!
•
When to use POST?
• Information sent from a form with the POST method is invisible to others (all names/values are
embedded within the body of the HTTP request) and has no limits on the amount of information to
send.
• Moreover POST supports advanced functionality such as support for multi-part binary input while
uploading files to server.
• However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Ad

More Related Content

Similar to Introduction to PHP and MySql basics.pptx (20)

Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
Sudhriti Gupta
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
KalGetachew2
 
oop.pptx
oop.pptxoop.pptx
oop.pptx
KabitaParajuli3
 
web programming Unit VI PPT by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Malothweb programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT by Bhavsingh Maloth
Bhavsingh Maloth
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
Sudip Simkhada
 
introduction to php and its uses in daily
introduction to php and its uses in dailyintroduction to php and its uses in daily
introduction to php and its uses in daily
vishal choudhary
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
baabtra.com - No. 1 supplier of quality freshers
 
Python programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops conceptPython programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
C++ in object oriented programming
C++ in object oriented programmingC++ in object oriented programming
C++ in object oriented programming
Saket Khopkar
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
AnsgarMary
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Bhanuka Uyanage
 
Unit 4-6 sem 7 Web Technologies.pptx
Unit 4-6 sem 7    Web  Technologies.pptxUnit 4-6 sem 7    Web  Technologies.pptx
Unit 4-6 sem 7 Web Technologies.pptx
prathameshp9922
 
Php
PhpPhp
Php
mohamed ashraf
 
Php
PhpPhp
Php
Siva Subramaniyan
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
web programming Unit VI PPT by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Malothweb programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT by Bhavsingh Maloth
Bhavsingh Maloth
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
introduction to php and its uses in daily
introduction to php and its uses in dailyintroduction to php and its uses in daily
introduction to php and its uses in daily
vishal choudhary
 
Python programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops conceptPython programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
C++ in object oriented programming
C++ in object oriented programmingC++ in object oriented programming
C++ in object oriented programming
Saket Khopkar
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
AnsgarMary
 
Unit 4-6 sem 7 Web Technologies.pptx
Unit 4-6 sem 7    Web  Technologies.pptxUnit 4-6 sem 7    Web  Technologies.pptx
Unit 4-6 sem 7 Web Technologies.pptx
prathameshp9922
 

Recently uploaded (20)

The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Ad

Introduction to PHP and MySql basics.pptx

  • 2. CONTENTS • Introduction to PHP and Mysql • XAMPP server • Basic PHP program • MySql in XAMPP • Login and User registration system.
  • 3. INTRODUCTION TO PHP AND MYSQL • The term PHP is an acronym for – Hypertext Preprocessor. PHP is a server-side scripting language designed specifically for web development also known as Personal Home Page. • It is open-source which means it is free to download and use. It is very simple to learn and use. The file extension of PHP is “.php”. • PHP can perform various tasks, including handling form data, generating dynamic page content, managing databases, and interacting with servers. • Features of PHP • DynamicTyping: PHP is dynamically typed, meaning you don’t need to declare the data type of a variable explicitly. • Cross-Platform: PHP runs on various platforms, making it compatible with different operating systems. • Database Integration: PHP provides built-in support for interacting with databases, such as MySQL, PostgreSQL, and others. • Server-Side Scripting: PHP scripts are executed on the server, generating HTML that is sent to the client’s browser.
  • 4. • A PHP script can be placed anywhere in the document.A PHP script starts with <?php and ends with ?> <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 5. • Any variables declared in PHP must begin with a dollar sign ($), followed by the variable name. • A variable can have long descriptive names (like $factorial, $even_nos) or short names (like $n or $f or $x) • A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9, and ‘_’) in their name. Even it cannot start with a number. • In PHP, both echo and print are language constructs used to output strings. echo can output multiple strings separated by commas without parentheses, while `print` can only output one string and always returns 1. • PHP is insensitive to whitespace. PHP is case-sensitive. All the keywords, functions, and class names in PHP (while, if, echo, else, etc) are NOT case-sensitive except variables.
  • 6. • PHP allows eight different types of data types. All of them are discussed below. There are pre- defined, user-defined, and special data types. • The predefined data types are: • Boolean • Integer • Double • String • The user-defined (compound) data types are: • Array • Objects • The special data types are: • NULL • Resource <?php // These are all valid declarations $val = 5; $val2 = 2.4; $val3 = 200.23; $x_Y = "gfg"; $_X = "GeeksforGeeks"; // This is an invalid declaration as it // begins with a number $10_ val = 56; // This is also invalid as it contains // special character other than _ $f.d = "num"; ?> $cars = array("Volvo","BMW","Toyota"); var_dump($cars); class Car { public $color; public $model; public function __construct($color, $model) { $this->color = $color; $this->model = $model; } public function message() { return "My car is a " . $this->color . " " . $this->model . "!"; } } $myCar = new Car("red", "Volvo"); var_dump($myCar);
  • 7. • Constants are either identifiers or simple names that can be assigned any fixed values.They are similar to a variable except that they can never be changed. • The define() function in PHP is used to create a constant as shown below: • define(name, value, case_insensitive) • The const keyword is used to declare a class constant. A constant is unchangeable once it is declared.The constant class declared inside the class definition. • You can also create constant variable using const keyword. const MYCAR = "Volvo"; • const vs. define() • const are always case-sensitive • define() has has a case-insensitive option. • const cannot be created inside another block scope, like inside a function or inside an if statement. • define can be created inside another block scope.
  • 8. • PHP provides us with four conditional statements: • if statement • if…else statement • if…elseif…else statement • switch statement • In PHP break is used to immediately terminate the loop and the program control resumes at the next statement following the loop. • The continue statement is used within a loop structure to skip the loop iteration and continue execution at the beginning of condition execution. It is mainly used to skip the current iteration and check for the next condition. • The continue accepts an optional numeric value that tells how many loops you want to skip. Its default value is 1.
  • 9. • PHP supports four types of looping techniques; 1. for loop 2. while loop 3. do-while loop 4. foreach loop
  • 10. • A function is a block of code written in a program to perform some specific task.A function name always begins with the keyword function. • Built-in functions : PHP provides us with huge collection of built-in library functions.These functions are already coded and stored in form of functions.To use those we just need to call them as per our requirement like, var_dump, fopen(), print_r(), gettype() and so on. • User Defined Functions :Apart from the built-in functions, PHP allows us to create our own customised functions called the user-defined functions. • Arrow functions, also known as “short closures”, is a new feature introduced in PHP 7.4 that provides a more concise syntax for defining anonymous functions.Arrow functions allow you to define a function in a single line of code, making your code more readable and easier to maintain.
  • 11. PHP OOP’S CONCEPT • Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. • Like C++ and Java, PHP also supports object oriented programming concepts like Classes and Objects, Encapsulation, Inheritance,Abstraction, Polymorphism, Interfaces. • Classes and Objects : 1.Classes are the blueprints of objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an:‘object’. 2.Class is a programmer-defined data type, which includes local methods and local variables. 3.Class is a collection of objects. Object has properties and behavior.
  • 12. • Constructors are the blueprints for object creation providing values for member functions and member variables. • Once the object is initialized, the constructor is automatically called. Destructors are for destroying objects and automatically called at the end of execution. • The constructor is defined in the public section of the Class. Even the values to properties of the class are set by Constructors. • Constructor types: • Default Constructor: It has no parameters, but the values to the default constructor can be passed dynamically. • Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.The -> operator is used to set value for the variables. In the constructor method, you can assign values to the variables during object creation. • Copy Constructor: It accepts the address of the other objects as a parameter. • Destructor is also a special member function which is exactly the reverse of constructor method and is called when an instance of the class is deleted from the memory. Destructors (__destruct ( void): void) are methods which are called when there is no reference to any object of the class or goes out of scope or about to release explicitly.
  • 13. They don’t have any types or return value. It is just called before de-allocating memory for an object or during the finish of execution of PHP scripts or as soon as the execution control leaves the block. <?php class SomeClass { function __construct() { echo "In constructor, "; $this->name = "Class object! "; } function __destruct() { echo "destroying " . $this->name . "n"; } } $obj = new Someclass(); ?>
  • 14. • Inheritance : • The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. • An inherited class is defined by using the extend keyword • Inherited methods can be overridden by redefining the methods (use the same name) in the child class. • The final keyword can be used to prevent class inheritance or to prevent method overriding. • PHP only supports single inheritance: a child class can inherit only from one single parent. • So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem. • Traits are used to declare methods that can be used in multiple classes.Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected). • Traits are declared with them trait keyword
  • 15. • In the PHP each and every property of a class in must have one of three visibility levels, known as public, private, and protected. • Public: Public properties can be accessed by any code, whether that code is inside or outside the class. If a property is declared public, its value can be read or changed from anywhere in your script. • Private: Private properties of a class can be accessed only by code inside the class. So if we create a property that’s declared private, only methods and objects inside the same class can access its contents. • Protected: Protected class properties are a bit like private properties in that they can’t be accessed by code outside the class, but there’s one little difference in any class that inherits from the class i.e. base class can also access the properties. Class Member Access Specifier Access from own class Accessible from derived class Accessible by Object Private Yes No No Protected Yes Yes No Public Yes Yes Yes
  • 16. • Abstract classes are the classes in which at least one method is abstract. Unlike C++ abstract classes in PHP are declared with the help of abstract keyword. • Use of abstract classes are that all base classes implementing this class should give implementation of abstract methods declared in parent class. An abstract class can contain abstract as well as non abstract methods. • Like Java, PHP also an instance of abstract class can not be created. • Like C++ or Java abstract class in PHP can contain constructor also. • An Interface is defined just like a class is defined but with the class keyword replaced by the interface keyword and just the function prototypes.The interface contains no data variables.The interface is helpful in a way that it ensures to maintain a sort of metadata for all the methods a programmer wishes to work on. • Few characteristics of an Interface are: • An interface consists of methods that have no implementations, which means the interface methods are abstract methods. • All the methods in interfaces must have public visibility scope. • Interfaces are different from classes as the class can inherit from one class only whereas the class can implement one or more interfaces.
  • 17. • Concrete Class:The class which implements an interface is called the Concrete Class. It must implement all the methods defined in an interface. Interfaces of the same name can’t be implemented because of ambiguity error. Just like any class, an interface can be extended using the extends operator • Namespaces are qualifiers that solve two different problems: 1. They allow for better organization by grouping classes that work together to perform a task 2. They allow the same name to be used for more than one class • For example, you may have a set of classes which describe an HTML table, such as Table, Row and Cell while also having another set of classes to describe furniture, such as Table, Chair and Bed. Namespaces can be used to organize the classes into two different groups while also preventing the two classes Table and Table from being mixed up.
  • 18. MYSQL MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. • The data in a MySQL database are stored in tables which consists of columns and rows. • MySQL is a database system that runs on a server. • MySQL is ideal for both small and large applications. • MySQL is very fast, reliable, and easy to use database system. It uses standard SQL • MySQL compiles on a number of platforms. PHP 5 and later can work with a MySQL database using: 1. MySQLi extension. 2. PDO (PHP Data Objects).
  • 19. • Difference Between MySQLi and PDO • PDO works on 12 different database systems, whereas MySQLi works only with MySQL databases. • Both PDO and MySQLi are object-oriented, but MySQLi also offers a procedural API. • If at some point of development phase, the user or the development team wants to change the database then it is easy to that in PDO than MySQLi as PDO supports 12 different database systems. He/She would have to only change the connection string and a few queries.With MySQLi, he/she will need to rewrite the entire code including the queries. • There are three ways of working with MySQl and PHP 1. MySQLi (object-oriented) 2. MySQLi (procedural) 3. PDO
  • 20. • Using MySQLi object-oriented procedure:We can use the MySQLi object-oriented procedure to establish a connection to MySQL database from a PHP script. • Syntax: <?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"; ?>
  • 21. • Using MySQLi procedural procedure :There is also a procedural approach of MySQLi to establish a connection to MySQL database from a PHP script as described below. • Syntax: <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
  • 22. • Using PDO procedure: PDO stands for PHP Data Objects.That is, in this method we connect to the database using data objects in PHP as described below: • Syntax: <?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
  • 23. • Using MySQLi object oriented procedure Syntax $conn->close(); • Using MySQLi procedural procedure Syntax mysqli_close($conn); • Using PDO procedure Syntax $conn = null;
  • 24. What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. The basic steps to create MySQL database using PHP are: • Establish a connection to MySQL server from your PHP script as described in this article. • If the connection is successful, write a SQL query to create a database and store it in a string variable. • Execute the query. • Using MySQLi Object-oriented procedure: If the MySQL connection is established using Object- oriented procedure then we can use the query() function of mysqli class to execute our query as described in the below syntax. • Using MySQLi Procedural procedure: If the MySQL connection is established using procedural procedure then we can use the mysqli_query() function of PHP to execute our query as described in the below syntax. • Using PDO procedure: If the MySQL connection is established using PDO procedure then we can execute our query as described in the below syntax.
  • 25. XAMPP SERVER The collection of related data is called a database. XAMPP stands for cross-platform,Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. Requirements: XAMPP web server procedure: • Start XAMPP server by starting Apache and MySQL. • Write PHP script for connecting to XAMPP. • Run it in the local browser. • Database is successfully created which is based on the PHP code. • In PHP, we can connect to the database using XAMPP web server by using the following path. localhost/phpMyAdmin
  • 26. • Insert • Select • Delete • Where • Update • OrderBy • GroupBy having • Limit In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set.The Limit Clause accepts one or two arguments which are offset and count.The value of both the parameters can be zero or positive integers. • Offset: It is used to specify the offset of the first row to be returned. Count: It is used to specify the maximum number of rows to be returned. • The Limit clause accepts one or two parameters, whenever two parameters are specified, the first is the offset and the second denotes the count whereas whenever only one parameter is specified, it denotes the number of rows to be returned from the beginning of the result set.
  • 27. PHP FORM HANDLING • GET vs. POST • Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. • Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. • $_GET is an array of variables passed to the current script via the URL parameters. • $_POST is an array of variables passed to the current script via the HTTP POST method.
  • 28. • When to use GET? • Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. • GET may be used for sending non-sensitive data. • Note: GET should NEVER be used for sending passwords or other sensitive information! • When to use POST? • Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. • Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. • However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
  翻译: