4.dynamic Web Site

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Dynamic Web Sites

with PHP & MYSQL


What Are Dynamic Web Sites?
Dynamic Web sites are flexible and potent creatures, more accurately described as

applications than merely sites.


Dynamic Web Sites
Dynamic Web sites Respond to different parameters (for example, the time of day or the
version of the visitor’s Web browser)

 Have a “memory,” allowing for user registration and login, e-commerce, and similar
processes.

 Almost always integrate HTML forms, allowing visitors to perform searches, provide
feedback, and so forth.

Often have interfaces where administrators can manage the site’s content.

Are easier to maintain, upgrade, and build upon than statically made sites.
What is pHp?
PHP is a “widely used general-purpose scripting language that is especially suited for

Web development and can be embedded into HTML.”


Why use pHp?
when it comes to developing dynamic Web sites, PHP is better, faster,
and easier to learn than the alternatives. What you get with PHP is
excellent performance, a tight integration with nearly every database
available, stability, portability, and a nearly limitless feature set due to
its extendibility. All of this comes at no cost (PHP is open source) and
with a very manageable learning curve
What is MySQL?
MySQL is the world’s most popular open source database application and is commonly used with
PHP. The MySQL software comes with the database server (which stores the actual data), different
client applications (for interacting with the database server), and several utilities. Like PHP,
MySQL offers excellent performance, portability, and reliability, with a moderate learning curve
and little to no cost.it is an open-source application, meaning that it is free to use or even modify.
MySQL database
Naming Database Elements
Before you start working with databases, you have to identify your needs. The purpose of
the Web site. When creating databases and tables, you should come up with names
(formally called identifiers) that are clear, meaningful, and easy to type. Also, identifiers
Should only contain letters, numbers, and the underscore (no spaces).
Should not be the same as an existing keyword (like an SQL term or a function name).
 Should be treated as case-sensitive.
Cannot be longer than 64 characters (approximately).
a table cannot have two columns with the same name and a database cannot have two
tables with the same name.
using phpMyAdmin
phpMyAdmin is one of the best and most popular applications written in PHP. Its sole purpose is to
provide an interface to a MySQL server. Access phpMyAdmin through your Web browser. The URL
you use will depend upon your situation. If running on your own computer, this might be
http://localhost/phpMyAdmin/. If running on a hosted site, your Web host will provide you with
the proper URL.
PHP Connect to MySQL
PHP 5 and later can work with a MySQL database using:

• MySQLi extension (the "i" stands for improved)

• PDO (PHP Data Objects)

Should I Use MySQLi or PDO?

PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you
have to switch your project to use another database, PDO makes the process easy. You only have to change the
connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.

Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for
web application security
PHP PDO

The PHP Data Objects (PDO) defines a lightweight interface for accessing databases in PHP. It
provides a data-access abstraction layer for working with databases in PHP. It defines consistent
API for working with various database systems.

The PDO represents a connection between PHP and a database server. The PDOStatement represents
a prepared statement and, after the statement is executed, an associated result set.
The PDOException represents an error raised by PDO.
MySQL database
To name a database’s elements:
1)Determine the database’s name.
This is the easiest and, arguably, least important step.
2)Determine the table names.
The table names just need to be unique within this database, which shouldn’t be a problem.
3)Determine the column names for each table.
The users table will have columns to store a user ID, a first name, a last name, an email address, a
password
The connection between PHP
and a database server
These variables are used to create a connection string to the database. The dsn is the Data
Source Name, which contains the information required to connect to the database.

A new PDO object is created. We pass the constructor the data source name and the user
name and password.
<?php
$dsn = 'mysql:host=localhost;dbname=web_site';
$username = 'root';
$password = '';
try {
$connection = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
$error_message= $e->getMessage();
echo ($error_message);
}
PHP PDO execution

The PDO exec executes an SQL statement and returns the number of affected rows.
require 'db.php';
$sql = 'SELECT * FROM users';
$statement = $connection->prepare($sql);
$statement->execute();
$people = $statement->fetchAll(PDO::FETCH_OBJ);

Prepared Statements protect from SQL injection, and are very important for web application
security.
Prepared Statements
<?php
include'index.php';
require 'connectDB.php';
$firstname=$_POST['firstN'];
$lastname=$_POST['lastN'];
$email=$_POST['email'];
$gender=$_POST['gender'];
INTO users (firstName, lastName, email, gender) VALUES
(:firstname, :lastname, :email, :gender)");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':gender', $gender);
$stmt->execute();
echo "you have insert record";
?>

You might also like