ThS Nguyễn Minh ViBM Tin học – ĐH An Giang PHP - MySQL GIỚI THIỆU... MySQL là hệ quản trị cơ sở dữ liệu ở phía server MySQL hỗ trợ SQL chuẩn... Biến $_GET chứa một mảng tên biến v
Trang 1ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP - MySQL
GIỚI THIỆU
Trang 2 PHP (Hypertext Preprocessor) là ngôn ngữ
script ở phía server
PHP hỗ trợ nhiều cơ sở dữ liệu (MySQL,
Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, )
Tập tin PHP:
có thể chứa text, các thẻ HTML và các đoạn script; nhưng kết quả khi trả về trình duyệt là HTML
có phần mở rộng là ".php"
Trang 3 MySQL là hệ quản trị cơ sở dữ liệu ở phía server
MySQL hỗ trợ SQL chuẩn
Trang 4 PHP kết hợp với MySQL có thể chạy
được trên nhiều platform khác nhau
PHP và MySQL hoàn toàn miễn phí
Một số hệ thống mã nguồn mở phổ biến:
Quản trị nội dung, cổng thông tin: Joomla, Drupal, PHP-Nuke, …
Diễn đàn: phpBB, MyBB, SMF, …
Thương mại điện tử: ZenCart, OpenCart, …
Đào tạo trực tuyến: Moodle, …
Blog: Wordpress, …
Trang 5 Để bắt đầu với PHP và MySQL, ta cần chuẩn bị:
Cài đặt web server (IIS, Apache, …)
Trang 6ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP
PHP Hypertext Preprocessor
Trang 10Ví dụ
<html>
<body>
<?php
//In ra chuỗi “Hello World”
echo "Hello World";
?>
</body>
</html>
Trang 12 Biến trong PHP không cần phải được khai báo trước, biến sẽ được tự động khai báo khi ta sử dụng nó
Tất cả các biến được bắt đầu bằng ký hiệu $
Tên biến:
có thể chứa các chữ cái, chữ số và dấu _
bắt đầu bởi ký tự chữ hoặc dấu _
không nên chứa khoảng trắng
Gán giá trị cho biến:
$var_name = value;
Trang 15 Biến $_GET chứa một mảng tên biến và giá trị được gởi bởi phương thức HTTP GET
Biến $_GET dùng để lấy giá trị từ form với
phương thức truyền dữ liệu method="get"
Thông tin gửi từ form với phương thức GET
được hiển thị trên thanh địa chỉ của trình duyệt, khối lượng dữ liệu gửi đi bị hạn chế
Trang 16 Biến $_POST chứa một mảng tên biến và giá trị được gởi bởi phương thức HTTP POST
Biến $_POST dùng để lấy giá trị từ form với
phương thức truyền dữ liệu method="post"
Thông tin gửi từ form bằng phương thức POST
sẽ không được người dùng nhìn thấy, khối
lượng dữ liệu truyền không hạn chế
Trang 18 dùng $GLOBALS hoặc từ khóa global
Trang 22 strlen(): độ dài chuỗi
strpos(): tìm chuỗi con hoặc ký tự trong
chuỗi
Trang 27Multidimensional Arrays
Mảng đa chiều: Mỗi phần tử của mảng
chính là một mảng con, mỗi phần tử trong mảng con có thể chứa một mảng con
khác, …
Trang 30% Modulus (division
remainder)
5%2 10%8 10%2
1 2 0
Trang 32Toán tử
So sánh:
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
Trang 33y=3 (x==5 || y==5) returns false
y=3
!(x==y) returns true
Trang 36}
Trang 40 Khai báo hàm với từ khóa function
Tên hàm có thể bắt đầu bằng một ký tự
hoặc dấu gạch dưới, không bắt đầu bởi số
Khối lệnh trong hàm được bao bởi cặp
dấu ngoặc nhọn {}
Trang 41function writeName ( $name ){
echo "Your name is " $name ; }
writeName ("Vi");
?>
</body>
</html>
Trang 42echo "1 + 16 = " add ( 1 , 16 );
?>
</body>
</html>
Trang 44Xử lý form
<html>
<body>
<form action = "welcome.php" method = "post" >
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
Welcome <?php echo $_POST[ "name" ]; ?> <br />
You are <?php echo $_POST[ "age" ]; ?> years old.
</body>
</html>
Welcome.php
Trang 45Xử lý form
Minh 22
Trang 46ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
MySQL
Trang 47Kết nối đến CSDL MySQL
Cú pháp:
mysql_connect( servername,
username, password);
Parameter Description servername Optional Specifies the server to connect
to Default value is "localhost:3306" username Optional Specifies the username to log
in with Default value is the name of the user that owns the server process password Optional Specifies the password to log in
with Default is ""
Trang 49Tạo mới
Tạo CSDL
Tạo bảng
CREATE DATABASE database_name
CREATE TABLE table_name (
column_name1 data_type, column_name2 data_type, column_name3 data_type,
… )
Trang 50if (mysql_query("CREATE DATABASE my_db",$con)) {
echo "Database created";
$sql = "CREATE TABLE person (
ID int NOT NULL AUTO_INCREMENT, FirstName varchar(15),
LastName varchar(15), Age int,
Trang 52Truy vấn
Chọn dữ liệu từ CSDL
Cú pháp
SELECT column_name(s) FROM table_name
Trang 53WHERE column operator value
Trang 54ORDER BY column_name DESC
Trang 56Cập nhật
Chỉnh sửa dữ liệu trong bảng CSDL
Cú pháp
UPDATE table_name SET column_name = new_value WHERE column_name = some_value
Trang 58ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP
NÂNG CAO
Trang 59PHP advance
Cookie
Session
ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP
Cookie
Trang 61 A cookie is a small file that the server
embeds on the user's computer
With PHP, you can both create and
retrieve cookie values
Cookies are part of the HTTP header, so cookies must be set before any output is sent to the browser
Trang 62 value: this value is stored on the clients computer
expire: the time the cookie expires, in seconds
path: the path on the server in which the cookie will
be available on
domain: the domain that the cookie is available
secure: the cookie will be set if a secure connection
exists (default is FALSE)
setcookie(name, value, expire, path, domain, secure);
Trang 64Retrieve a cookie value
Use the PHP $_COOKIE variable
Ex:
<?php
// Print an individual cookie
echo $_COOKIE [ "TestCookie" ];
// Another way to debug/test is to view all cookies
print_r ( $_COOKIE );
?>
Trang 65Delete a cookie
Assure that the expiration date is in the past
<?php
// set the expiration date to one hour ago
setcookie ( "TestCookie" , "" , time () - 3600 );
?>
Trang 66ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP
Session
Trang 67 A PHP session allowing you to store user
information on the server for later use, session information is temporary and will be deleted after the user has left the website
Sessions work by creating a unique id (UID) for each visitor and store variables based on this
UID
Trang 68Starting a PHP Session
The session_start() function
place before the <html> tag
<?php
session_start ();
?>
Trang 69Storing a Session Variable
Use the PHP $_SESSION variable
<?php
session_start ();
if ( isset ( $_SESSION [ 'views' ]))
$_SESSION [ 'views' ] = $_SESSION [ 'views' ] + 1 ;
else
$_SESSION [ 'views' ] = 1 ;
//retrieve session data
echo "Pageviews=" $_SESSION [ 'views' ];
?>
Trang 70Destroying a Session
The unset() function is used to free the
specified session variable
The session_destroy() function is used to destroy the session completely
Trang 71ThS Nguyễn Minh Vi
BM Tin học – ĐH An Giang
PHP
mail() function
Parameters (cont.)
headers (optional) specifies additional
headers, like From, Cc, and Bcc The
additional headers should be separated with a CRLF (\r\n)
parameters (optional) specifies an additional parameter to the sendmail program
Trang 74<?php
$to = "bibi@gmail.com" ;
$subject = "Gui mail bang PHP" ;
chua text." ;
echo "Da gui." ; else
echo "Khong gui duoc" ;
?>
Trang 77Configuration
DNS name or IP address of the SMTP server PHP should use for mail sent with the mail() function
smtp_port
Number of the port to connect to the server
specified with the SMTP setting when sending
mail with mail(); defaults to 25 (available since PHP 4.3.0)