4.dynamic Web Site
4.dynamic Web Site
4.dynamic Web Site
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
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";
?>