+ All Categories
Home > Technology > Php modul-3

Php modul-3

Date post: 07-Jul-2015
Category:
Upload: kristophorus-hadiono
View: 1,307 times
Download: 3 times
Share this document with a friend
24
PHP TUTORIAL PHP and Database
Transcript
Page 1: Php modul-3

PHP TUTORIAL

PHP and Database

Page 2: Php modul-3

What is Database?

● A database is an organized collection of data.

● The data collection are typically organized to model relevant aspects of reality in a way that supports processes requiring this information

Page 3: Php modul-3

What is Database Management System?

● Database management systems (DBMSs) are specially designed applications that interact with the user, other applications, and the database itself to capture and analyze data.

● A general-purpose database management system (DBMS) is a software system designed to allow the definition, creation, querying, update, and administration of databases.

Page 4: Php modul-3

Why use database?

● Databases are most useful when it comes to storing information that fits into logical categories.

● Example: store employees record– Supervisors table, managers tables, suppliers

table, etc.

Page 5: Php modul-3

Why MySQL?

● The most popular database system used with PHP.● PHP combined with MySQL are cross-platform (you

can develop in Windows and serve on a Unix platform)● Most web hosts do not allow you to create a database

directly through a PHP script.● Instead they require that you use the PHP/MySQL

administration tools on the web host control panel to create these databases

Page 6: Php modul-3

What is “Queries”

● A query is a question or a request.● We can query a database for specific

information and have a recordset returned● Example:

– SELECT * FROM customers;

Page 7: Php modul-3

How to learn SQL

● Remember this:– SQL language is not complicated

● There are many keywords in MySQL, and a good programming habit when using ANY of these words is to capitalize them.

● This helps draw them out from the rest of the code and makes them much easier to read.

● Example:– SELECT * FROM example

Page 8: Php modul-3

SQL Basic Command

● Create Database– CREATE DATABASE <database_name>

● Use Database– Use <database_name>

● Create Table– CREATE TABLE <table_name> (col1 type1, col2 type2, … , colx

typex)

● Example– CREATE DATABASE exercise;

– Use exercise;

– CREATE TABLE student (name varchar(30), age INT);

Page 9: Php modul-3

SQL Basic Command

● Insert data to table– INSERT INTO <table_name> (col1,col2, … , colx)

VALUES (val1, val2, … , valx);

– Example:● INSERT INTO student VALUES ('William',27);

● Query data from table– SELECT <colx| *> FROM <table_name> WHERE

<expression>

– Example:● SELECT name FROM student WHERE age>20;

Page 10: Php modul-3

SQL Basic Command

● Edit data– UPDATE <table_name> SET <colx=valx> WHERE

<expression>

● Delete data– DELETE FROM <table_name> WHERE <expression>

● Example– UPDATE student SET age=30 WHERE

name='William';

– DELETE FROM student WHERE name='William';

Page 11: Php modul-3

PHP Form Handling

● Use $_GET or $_POST to handle the data from the form● Example (form_get.html)

<html>

<body>

<form action="helloget.php" method="get">

Name: <input type="text" name="name"><br>

<input type="submit">

</form>

</body>

</html>

Page 12: Php modul-3

PHP Form Handling

● Helloget.php

<html>

<body>

Welcome <?php echo $_GET["name"]; ?>

</body>

</html>

Page 13: Php modul-3

PHP Form Handling

● (form_post.html)

<html>

<body>

<form action="hellopost.php" method="post">

Name: <input type="text" name="name"><br>

<input type="submit">

</form>

</body>

</html>

Page 14: Php modul-3

PHP Form Handling

● Hellopost.php

<html>

<body>

Welcome <?php echo $_POST["name"]; ?>

</body>

</html>

Page 15: Php modul-3

PHP Form Handling

● GET vs. POST– $_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.

Page 16: Php modul-3

PHP Form Handling

● When we use GET method?– Remember this : Information sent from a form

with the GET method is visible to everyone (in address bar).

– GET has limits on the amount of information to send (only 2000 character).

– ONLY USED for sending non-sensitive data.

– Never use GET to send sensitive informations such as username or password

Page 17: Php modul-3

PHP Form Handling

● When we use POST method?– Remember this : Information sent from a form

with the POST method is invisible to others.

– POST has no limits on the amount of information to send.

– Supports advanced functionality such as multi-part binary input while uploading files to server.

Page 18: Php modul-3

PHP & MySQL

● Open connection to MySQL– mysqli_connect(host, username, password,

database_name)

– Example

$con=mysqli_connect(“localhost”,”root”,”1234”,”exercise”);

● Check connection

if (mysqli_connect_errno($con))

echo "Failed to connect to MySQL: " . mysqli_connect_error();

Page 19: Php modul-3

PHP and MySQL

● Close connection– mysqli_close(connection_variable)

● Example– mysqli_close($con);

Page 20: Php modul-3

PHP and MySQL

● Send query to MySQL– mysqli_query(<query>,<connection_variable>

);

● Example:

$sql=”INSERT INTO student VALUES('John',33)”;

mysqli_query($sql,$con);

Page 21: Php modul-3

PHP and MySQL

● Retrieve result of query

$sql=”SELECT * FROM student”;

$result=mysqli_query($sql,$con);

While ($row = mysqli_fetch_array($result)) {

echo $row['name'] . “ “ . $row['age'] . “<br>”;

}

Page 22: Php modul-3

excercise

● A. Database– Use database exercise

– Create a course table which have structure like this:● id_course – varchar● name_course – varchar● Credit – int

– Create a student table which have structure like this:● id_student – varchar● Firstname – varchar● Lastname - varchar

– Insert 10 data into table course and student

Page 23: Php modul-3

excercise

● B. PHP and MySQL– Create a web application with php to handles

● The input for table course and student● List all data for table course and student● Search data for table course and student

Page 24: Php modul-3

Home Works

● Create a web applications with PHP to handle the update and delete proses for table course and student


Recommended