HáťC TáťT TIáşžNG ANH 11 THEO CHĆŻĆ NG TRĂNH GLOBAL SUCCESS ÄĂP ĂN CHI TIáşžT - CẢ NÄ...
Â
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', 'john@example.com')";
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;
}