Web II (PHP) (12)
Web II (PHP and MySQL Database Connectivity)
What
is PHP?
The
PHP Hypertext Preprocessor (PHP) is a programming language that allows web
developers to create dynamic content that interacts with databases. PHP is
basically used for developing web based software applications. It is a
widely-used, free and opensource scripting language. The main feature of PHP
from something like client-side JavaScript is that the code is executed on the
server, generating HTML which is then sent to the client.
Basic
Syntax of PHP
<html>
<head>
<title>Hello
World</title>
</head>
<body>
<? php
echo "Hello, World!" ;
?>
</body>
</html>
Character
functions in PHP
strlen(
):
Counting the number of character inside the string.
strlen($my_str);
str_word_count(
):
Counting the number of words in a string.
str_word_count(my_str);
str_replace(
):
Replaces the searched text inside the string.
str_replace("facts",
"truth", $my_str);
strrev(
):
reverses the string.
strrev($my_str);
Get
and Post Methods in PHP
There
are two ways the browser client can send information to the web server.
GET
Mehod
The
GET method is used to submit the HTML form data. This data is collected by the predefined
$_GET variable for processing. The information sent from an HTML form using the
GET method is visible to everyone in the browser's address bar,
Advantages
of GET method
You can bookmark the page with the specific query string because the data sent
by the GET method is displayed in URL.
GET requests can be cached.
GET requests are always remained in the browser history.
Disadvantages
of GET Method
The GET method should not be used while sending any sensitive information.
A
limited amount of data can be sent using method = "get". This limit
should not exceed 2048 characters.
For security reasons, never use the GET method to send highly sensitive
information like username and password, because it shows them in the URL.
The GET method cannot be used to send binary data (such as images or word documents)
to the server.
POST
Method
POST
method is also used to submit the HTML form data. But the data submitted by
this method is collected by the predefined superglobal variable $_POST instead
of $_GET. GET method, it does not have a limit on the amount of information to
be sent.
Advantages
of POST method
The POST method is useful for sending any sensitive information because the information
sent using the POST method is not visible to anyone.
There is no limitation on size of data to be sent using the POST Method. You
can send a large amount of information using this method.
Binary and ASCII data can also be sent using the POST method.
Data security depends on the HTTP protocol because the information sent using
the POST method goes through the HTTP header. By using secure HTTP, you can
ensure that your data is safe.
Disadvantages
of POST Method
POST requests do not cache.
POST requests never remain in the browser history.
It
is not possible to bookmark the page because the variables are not displayed in
URL.
What is -> in PHP?
This is referred to as the object operator, or sometimes
the single arrow operator. It is an access operator used for access/call
methods and properties in a PHP object in Object-Oriented Programming (OOP)
Database
Connectivity
Index.php
<html>
<body>
<form
action = "insert.php" method = "post">
Username:
<input type = "text" name = "username" /> <br>
Blood Group:
<input type = "text" name= "bloodgroup" /><br>
<input type
= "submit" />
<br>
</form>
<form
action="display.php"method="post">
<br>
Serial No.:
<input type="text" name="sno">
<br>
<input
type="submit">
</form>
</body>
</html>
connect.php
<?php
$servername =
"localhost";
$username =
"root";
$password =
"";
$dbname =
"amc";
// Create
connection
$conn = new
mysqli($servername,$username,$password,$dbname);
// Check
connection
if
($conn->connect_error) {
die("Connection failed: " .
$conn->connect_error);
}
echo "Connected successfully";
?>
insert.php
<?php
include("connect.php");
// Inserting
data into table
$sname=$_REQUEST['username'];
$blood_group=$_REQUEST['bloodgroup'];
$sql =
"INSERT INTO registration (name, blood_group)
VALUES
('$sname', '$blood_group')";
if
($conn->query($sql) === TRUE) {
echo "<br>"."New record
added successfully";
} else {
echo "Error: " . $sql .
"<br>" . $conn->error;
}
$conn->close();
?>
display.php
<?php
include("connect.php");
//displaying
data from table to html
$sql = "SELECT sno, name, blood_group
FROM registration";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data
of each row
echo
"<br> Your searched result is <br>";
while($row = $result->fetch_assoc()) {
$sno=$_REQUEST['sno'];
if($row["sno"]=="$sno")
{
echo"<table
border=1>
<tr>
<th>
S.No. </th>
<th>
Name </th>
<th>
Blood Group</th>
</tr>
<tr>
<td>".$row["sno"]."</td>
<td>".$row["name"]."</td>
<td>".$row["blood_group"]."</td>
</table>"
;
}
} } else { echo "0 results"; }
?>
Learn to understand above code
mysqli( )
The MySQLi functions allows you to access MySQL database
servers.
connect_error
Return the error description from the last connection
error, if any
close( )
Close a previously opened database connection:
die( )
The die() is an inbuilt function in PHP. It is used to
print message and exit from the current php script. It is equivalent to exit()
function in PHP. Syntax :
die($message)
fetch_assoc( )
Fetch a result row as an associative array:
error( )
Return the last error description for the most recent function
call, if any.
num_rows
Return the number of rows in a result set.
$_REQUEST
$_REQUEST is a PHP super global variable which is used to
collect data after submitting an HTML form.
SQL
statements
SQL
SELECT
Statement
SELECT sno, name, blood_group FROM
registration;
SQL
DISTINCT
Clause : used to return only distinct (different) values.
SELECT DISTINCT sno, name,
blood_group FROM table_name;
SQL
WHERE
Clause
SELECT sno, name, blood_group FROM registration WHERE blood_group='B +ve';
SQL
AND/OR
Clause
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';
SQL
IN
Clause
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SQL
BETWEEN
Clause
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
SQL
LIKE
Clause
Starting with 'a':
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
Ending with 'a': LIKE '%a'
That have "or" in any
position: LIKE '%or%';
That have "r" in the
second position: LIKE '_r%';
Starts with "a" and are
at least 3 characters in length: LIKE 'a__%';
Starts with "a" and ends
with "o": LIKE 'a%o';
That does NOT start with
"a": NOT LIKE 'a%';
SQL
ORDER BY
Clause
SELECT * FROM Customers
ORDER BY Country;
SQL
GROUP BY
Clause
SELECT COUNT(CustomerID),
Country FROM Customers GROUP BY Country;
SQL
COUNT
Clause
SELECT COUNT(ProductID)
FROM Products;
SQL
HAVING
Clause
SELECT COUNT(CustomerID),
Country FROM Customers GROUP BY Country
HAVING COUNT(CustomerID)
> 5;
SQL
CREATE TABLE
Statement
CREATE TABLE Persons ( PersonID int, LastName
varchar(255), FirstName varchar(255),
Address varchar(255), City varchar(255));
SQL
DROP TABLE
Statement : deletes a table
DROP TABLE Shippers;
SQL
CREATE INDEX
Statement
CREATE INDEX idx_pname
ON Persons (LastName, FirstName);
SQL
DROP INDEX
Statement
ALTER TABLE Person
DROP INDEX idx_pname;
SQL
DESC
Statement: to sort the data returned in descending order.
SELECT * FROM Customers
ORDER BY CustomerName DESC;
SQL
TRUNCATE TABLE
Statement: deletes the data inside a table
TRUNCATE TABLE Categories;
SQL
ALTER TABLE
Statement
ALTER TABLE Customers
ADD Email varchar(255);
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE Customers
MODIFY Email varchar(200);
SQL
ALTER TABLE
Statement (Rename)
ALTER TABLE employees RENAME COLUMN
id TO employ_id;
ALTER TABLE
Student RENAME TO Student_Details;
SQL
INSERT INTO
Statement
INSERT INTO Customers (CustomerName,
ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
SQL
UPDATE
Statement
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
SQL
DELETE
Statement
DELETE FROM Customers WHERE CustomerName='Alfreds
Futterkiste';
SQL
CREATE DATABASE
Statement
CREATE DATABASE testDB;
SQL
DROP DATABASE
Statement
DROP DATABASE testDB;
SQL
USE
Statement
USE candy;
SQL
COMMIT
Statement
DELETE from
Customer where State = 'Texas';
COMMIT;
SQL
ROLLBACK
Statement
DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
Comments
Post a Comment