PHP/MySQL Tutorial
ễ
Nguy n Quang Hùng Email: hungnq2@dit.hcmut.edu.vn
Web site: http://www.dit.hcmut.edu.vn/~hungnq/courses.htm
ụ
M c tiêu
ắ
ộ
ệ ậ N m b t công ngh l p trình trang web đ ng
ắ ằ b ng PHP.
ể
ề
ấ
Tìm hi u v cách truy v n CSDL MySQL t
ừ
PHP.
ộ ứ
ạ ệ
ụ
ứ
Vi
ế t m t ng d ng Tra c u danh b đi n ạ ằ
tho i b ng JSP.
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
ậ
L p trình web phía server
ậ
ớ
ự ọ
L p trình Web v i CGI (t
h c)
ậ
ự ọ
ớ L p trình Web v i ASP/ASP.NET (t
h c)
ậ
ớ
ự ọ
L p trình Web v i Servlet (t
h c)
ậ
ớ
ươ
L p trình Web v i JSP (ch
ng 7)
ậ
ớ L p trình Web v i PHP (hôm nay)
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Introduction
PHP (Hypertext Preprocessor)
Open source, serverside, scripting language.
Supports databases such as MySQL and Oracle.
http://www.w3schools.com/php/default.asp
MySQL (Structured Query Language)
Open source, speedy, scalable, reliable database
technology.
http://dev.mysql.com/doc/mysql/en/Tutorial.html
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Tutorial Overview
Database (MySQL)
DB creation
Add/delete tables
Add/delete/update records
View/query records
Web (PHP)
User frontend
Add & query code
Delete & update code
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
MySQL & TCD
(1)
‘PuTTY’ into wilde.cs.tcd.ie on port 22 w/ SSH
Authenticate with your TCD username and password
(2) ssh macneil.cs.tcd.ie (password as above)
(3) Login into your MySQL account
‘mysql –uUSERNAME –p’
Enter your MySQL username and password
(4) Use MySQL syntax to create and view table(s),
records, etc.
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Basic MySQL Syntax
SHOW DATABASES;
USE database_name;
SHOW TABLES;
DROP TABLE table_name;
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Create MySQL Table
CREATE TABLE user (
name varchar(9) NOT NULL,
id int(6) NOT NULL,
PRIMARY KEY (id),
UNIQUE (id)
);
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Add/Delete/Update Table
INSERT INTO user VALUES (‘bond’, ‘007’);
DELETE FROM user WHERE id=‘007’;
UPDATE user SET name=‘BOND’ WHERE
id=‘007’;
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Query Database
SELECT * FROM user;
SELECT * FROM user WHERE name=‘BOND’;
SELECT DISTINCT name FROM user;
SELECT name, id FROM user ORDER BY name;
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
MySQL Administrator GUI tool
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
PHP User FrontEnd
$variable=“271004";
echo $variable;
?>
Script is executed server side and presented to user via a
browser.
PHP code is rendered as plain HTML.
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
PHP Configuration File
// configuration parameters
// database configuration
• Use a securely positioned ‘config’ file to store variables.
$host = "macneill.cs.tcd.ie";
$user = “username";
$pass = “password";
$db = “username_db";
// default contact person
• Other PHP pages can link to it and use the variables as their own.
$def_contact = “Karl";
?>
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
PHP Add to DB Code 1
PHP Add to DB Code 2
// form submitted so start processing it
include("conf.php");
// set up error list array & validate text input fields
$errorList = array();
$count = 0;
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }
// set default value for contact person
if (!$contact) { $contact = $def_contact; }
// check for errors & if none found...
if (sizeof($errorList) == 0)
$title = $_POST["title"]; $authors = $_POST["authors"];
mysql_select_db( $db ) or die ("Unable to select database!"); $query = "INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre)
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
VALUES ('$title', '$authors', '$description', '$comment',
'$super','$bibtex','$url','$genre')";
echo "Addition successful.
Go back to the main page | home";
// close database connection
$result = mysql_query( $query ) or die ("Error in query: $query. " . mysql_error());
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
else {// errors occurred}
?>
mysql_close($connection); }
PHP Query Code
include("conf.php");
$connection = mysql_connect($host, $user, $pass) or die ();
mysql_select_db($db) or die ("Unable to select database!");
$query = "SELECT * FROM papers";
$result = mysql_query($query) or die ("Error in query”); ?>
height="2"> |
PHP Update Code 2
include("conf.php");
// form submitted so start processing it
$title = $_POST["title"];
$authors = $_POST["authors"];
$id = $_POST["id"];
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }
if (!$contact) { $contact = $def_contact; }
// check for errors, if none found...
if (sizeof($errorList) == 0)
{
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// print result
echo "Update successful. href=http://www.cs.tcd.ie/Karl.Quinn/>home
Go back to the main page |
// close database connection
mysql_close($connection);
}
else{}
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
?>
$query = "UPDATE papers SET title = '$title', authors = '$authors', description = '$comment', super = '$super', bibtex = '$bibtex', url = '$url', genre = '$description', comment = '$genre' WHERE id = '$id'";
ệ
ả Tài li u tham kh o
1.
http://www.w3schools.com/php/php_forms.asp
2.
http://www.w3schools.com/php/php_mysql_intro.asp
3.
http://www.php.net/mysql
4.
http://www.w3schools.com/php/php_mysql_intro.asp
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Brief History of PHP
PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially
developed for HTTP usage logging and serverside form generation in Unix.
PHP 2 (1995) transformed the language into a Serverside embedded scripting language.
Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc.
PHP 3 (1998) added support for ODBC data sources, multiple platform support, email
protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans .
PHP 4 (2000) became an independent component of the web server for added efficiency.
The parser was renamed the Zend Engine. Many security features were added.
PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Brief History of PHP
As of August 2004, PHP is used on 16,946,328 Domains, 1,348,793 IP Addresses http://www.php.net/usage.php This is roughly 32% of all domains on the web.
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Why is PHP used?
1. Easy to Use
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".
Why is PHP used?
2. Cross Platform
Runs on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases
Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server
Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003
Supported Databases: Adabas D, dBase,Empress, FilePro (read- only), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
Why is PHP used?
3. Cost Benefits
PHP
Software
Free
Platform
Free (Linux)
Development Tools
Free
PHP Coder, jEdit
Nguyễn Quang Hùng – E-mail: hungnq2@dit.hcmut.edu.vn
PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free.
Getting Started
1. How to escape from HTML and enter PHP mode
PHP parses a file by looking for one of the special tags that tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag.
PHP CODE
HTML
HTML
Starting tag
Ending tag
Notes
Preferred method as it allows the use of PHP with XHTML
?>
Not recommended. Easier to type, but has to be enabled and may conflict with XML