0% found this document useful (0 votes)
28 views

Web Technology and DBMS

The document discusses using PHP and MySQL together for web development. It covers connecting to a MySQL database from PHP, performing basic CRUD operations, and using PHP to power dynamic HTML pages. It provides examples of retrieving and displaying data from MySQL in PHP scripts.

Uploaded by

Untul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Web Technology and DBMS

The document discusses using PHP and MySQL together for web development. It covers connecting to a MySQL database from PHP, performing basic CRUD operations, and using PHP to power dynamic HTML pages. It provides examples of retrieving and displaying data from MySQL in PHP scripts.

Uploaded by

Untul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

MK Pengantar Basis Data

Program Studi S1 Teknologi Sains Data


Fakultas Teknologi Maju dan Multidisiplin
Universitas Airlangga

14. Web Technology and DBMS


M. N. Fakhruzzaman, S.Kom., M.Sc.
Dr. Maryamah, S.Kom.
Outline
• Introduction
• Tutorial
• Installation
• MySQL with PHP
Introduction
• A Database is useless without a usecase
• Typically, database is the backbone for web applications
• Simple forms – such as sign up and log in forms – uses database and a
simple html interface
• PHP is often required to power up html page – by adding a server-side
script processing capabilities
• PHP is often the brain of HTML in web technologies
Introduction
• PHP (recursive acronym for "PHP: Hypertext
Preprocessor")
▫ widely-used Open Source general-purpose scripting language
▫ especially suited for Web development
▫ can be embedded into HTML.
<html>
<head>
<title>Example</title>
</head>
<body>
<?php echo "Hi, I'm a PHP script!"; ?>
</body>
</html>
What can PHP do?
• Server-side scripting
▫ most traditional and main target field
▫ need three things to make this work
● PHP parser (CGI common gateway interface or server module)
● webserver – with connected PHP installation (or use XAMPP)
● web browser
• Command line scripting
▫ PHP script to run it without any server or browser
▫ need the PHP parser to use it this way
Tutorial
• What do I need?
• Your first PHP-enabled page
• Something Useful
• Dealing with Forms
• What's next?
What do I need?
• Assume that your server has support for PHP activated
and that all files ending in .php are handled by PHP
• Create your .php files and put them in your web directory
and the server will magically parse them for you
• No need to compile anything
• Develop locally
▫ install a web server, such as Apache
▫ install PHP
▫ install a database as well, such as MySQL
First PHP-enabled page
• Create a file named hello.php
• Put it in your web servers directory
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo "<p>Hello World</p>"; ?>
</body>
</html>
• Use your browser to access the file
Something useful
• check what sort of browser the person viewing the
page is using
▫ check the user agent string that the browser sends as part
of its HTTP request
▫ stored in a variable
● always start with a dollar-sign in PHP
● $_SERVER["HTTP_USER_AGENT"].
● $_SERVER is a special reserved PHP variable that contains all
web server information
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>
</body>
</html>
Variables
• Variables represented by a dollar sign followed
by the name of the variable
▫ name is case-sensitive.
▫ name starts with a letter or underscore, followed
by any number of letters, numbers, or underscores
▫ type is assigned by value
<?php
$var = "Bob";
$Var = "Joe";
echo "$var, $Var"; // outputs "Bob, Joe"
$x = 1;
$x = ‘abc’; // type can change if value changes
$4site = 'not yet'; // invalid; starts with a number $_4site
= 'not yet'; // valid; starts with an underscore
?>
Arrays
• created by the array() language-construct
• takes a certain number of comma-separated key =>
value pairs
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
Dealing with forms
• HTML Forms (GET and POST)
▫ form is submitted to a PHP script
▫ information from that form is
automatically made available to the
script
▫ forms.php
<form action="foo.php" method="POST">
Name: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
<input type="submit" name="submit"
value="Submit me!">
</form>
Forms – foo.php
<?php // Available since PHP 4.1.0

print $_POST['username'];
print $_REQUEST['username'];
import_request_variables('p', 'p_');
print $p_username;

// Available since PHP 3. As of PHP 5.0.0, these long


// predefined variables can be disabled with the
// register_long_arrays directive.

print $HTTP_POST_VARS['username'];

// Available if the PHP directive register_globals = on.


// As of PHP 4.2.0 the default value of
// register_globals = off.
// Using/relying on this method is not preferred.

print $username; ?>


Another forms example
• info_form.php
<form action=“show_answers.php” method="POST">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="submit">
</form>
• show_answers.php
Hi
<?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.
15

Connecting to MySQL
• Before accessing the DB, we have to open a connection to the DB
• Use this to open a connection
mysql_connect(host,username,password)
• Use this to close the connection (for security)
mysql_close(pengenal_hubungan);

In newer versions, mysqli_ is preferred


16

Selecting DB

• After opening a connection, you can select which db to use


• Use this to select DB:
mysql_select_db(database_name,connection_token)
where connection_token can be acquired by saving
the mysql_connect returns in a variable
17

Accessing a table

• Suppose there’s a Table1 in db Test


• Table1 has this structure:
nama varchar (20)
umur int(2)
• With their values:
Sarjono 35
Budiman 18
Joko Waluyo 21
Bambang Sudiyono 28
18

• To use a query:
mysql_query(query,token)
• Count column:
mysql_num_fields
(queryresult_returns)
• Count records:
mysql_num_rows
(queryresult_returns)
19

Reading Table (R in CRUD)

• To access records in query results:


$baris=mysql_fetch_row(queryresult_returns)
• $baris is an array, accessible by its index $baris[0], etc.
20

Access field using mysql_fetch_field


 To access field (column):
mysql_fetch_field(queryresult,
columnindex)
Fetched object can be specified further :
->name
->max_length
21

Create (C in CRUD)
• SQL for inserting:
INSERT INTO `tablename`
(`column1’, `column2`,..)
VALUES (‘value1’, ‘value2‘,..)";

e.g.
INSERT INTO `table1` (`nama`,
`umur`) VALUES ('Andi', '15')";
22

Delete (D in CRUD)
DELETE FROM `tablename` WHERE
‘columname’=‘value‘
• E.g.
• DELETE FROM `tabel1` WHERE
`nama`=‘Andi’  menghapus
semua record jika nama=‘Andi’
Conclusion
• Using mysql_connect, PHP can communicate with database, in any
servers
• Hence, PHP can be used mostly to CRUD any data

• Using a simple combination of PHP, MySQL, HTML, and CSS, you can
build a powerful (and dynamic) web app
• Add some javascripts, and you can do Asynchronous processing

• However, using single script to open a connection and doing query


impose security risk (sql injection)
• More secure approach involves parameterization, Model-View-
Controller paradigm, and using APIs
Thank You ☺

You might also like