PHP TUTORIAL
PHP and Database
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
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.
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.
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
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;
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
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);
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;
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';
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>
PHP Form Handling
● Helloget.php
<html>
<body>
Welcome <?php echo $_GET["name"]; ?>
</body>
</html>
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>
PHP Form Handling
● Hellopost.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>
</body>
</html>
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.
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
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.
PHP & MySQL
● Open connection to MySQL
– mysqli_connect(host, username, password,
database_name)
– Example
$con=mysqli_connect(“localhost”,”root”,”1234”,”exercis
e”);
● Check connection
if (mysqli_connect_errno($con))
echo "Failed to connect to MySQL: " .
mysqli_connect_error();
PHP and MySQL
● Close connection
– mysqli_close(connection_variable)
● Example
– mysqli_close($con);
PHP and MySQL
● Send query to MySQL
– mysqli_query(<query>,<connection_variable>
);
● Example:
$sql=”INSERT INTO student
VALUES('John',33)”;
mysqli_query($sql,$con);
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>”;
}
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
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
Home Works
● Create a web applications with PHP to
handle the update and delete proses for
table course and student

Php modul-3

  • 1.
  • 2.
    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
  • 3.
    What is DatabaseManagement 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.
  • 4.
    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.
  • 5.
    Why MySQL? ● Themost 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
  • 6.
    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;
  • 7.
    How to learnSQL ● 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
  • 8.
    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);
  • 9.
    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;
  • 10.
    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';
  • 11.
    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>
  • 12.
    PHP Form Handling ●Helloget.php <html> <body> Welcome <?php echo $_GET["name"]; ?> </body> </html>
  • 13.
    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>
  • 14.
    PHP Form Handling ●Hellopost.php <html> <body> Welcome <?php echo $_POST["name"]; ?> </body> </html>
  • 15.
    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.
  • 16.
    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
  • 17.
    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.
  • 18.
    PHP & MySQL ●Open connection to MySQL – mysqli_connect(host, username, password, database_name) – Example $con=mysqli_connect(“localhost”,”root”,”1234”,”exercis e”); ● Check connection if (mysqli_connect_errno($con)) echo "Failed to connect to MySQL: " . mysqli_connect_error();
  • 19.
    PHP and MySQL ●Close connection – mysqli_close(connection_variable) ● Example – mysqli_close($con);
  • 20.
    PHP and MySQL ●Send query to MySQL – mysqli_query(<query>,<connection_variable> ); ● Example: $sql=”INSERT INTO student VALUES('John',33)”; mysqli_query($sql,$con);
  • 21.
    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>”; }
  • 22.
    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
  • 23.
    excercise ● B. PHPand 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
  • 24.
    Home Works ● Createa web applications with PHP to handle the update and delete proses for table course and student