yet another line of comment */echo"This is yet another test"; print "Hello World"; print"Hello World"; ?>... Phép toán 29 Phép toán... code to be executed if expression = label1;echo "Nu
Trang 1Thanh Ngh
dtnghi@cit.ctu.edu.vn
C n Th 24-11-2005
Khoa Công Ngh Thông Tin
PHP k t h p v i forms Cookies, SSI (Server side includes), Date PHP-MySQL
Trang 2Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n Hàm
PHP k t h p v i forms Cookies, SSI (Server side includes), Date PHP-MySQL
S d ng ph n m r ng tên file : php, phtml PHP scripts s tr v k t qu cho trình duy t m t plain HTML
Trang 3Gi i thi u v PHP
5
MySQL là gì ?
H qu n tr c s d li u Dùng cho các ng d ng v a và nh
Tư ng thích v i h u h t các web server (Apache, IIS, etc)
D h c và phát tri n nhanh các ng d ng trên Web
Trang 4Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n Hàm
PHP k t h p v i forms Cookies, SSI (Server side includes), Date PHP-MySQL
7
Cú pháp PHP
8
Cú pháp
PHP scripts ch a text, th HTML, script
Ví d : in ra màn hình chu i “Hello World”
Trang 5yet another line of comment */
echo("This is yet another test");
print "Hello World";
print("Hello World");
?>
Trang 6Tên bi n b!t u b%ng m t ký t& ch cái ho"c _
Phân bi t gi a ký t& thư'ng và hoa
echo "$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
?>
Trang 7$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar
echo $foo; // My name is Bob
$foo = array(1, 2, 3, 4, 5);
for($i = 0; $i < 5; $i++) echo $foo[$i] "<br>";
?>
Trang 8Bi n
15
Bi n có s(n trong PHP
$GLOBALS : t#t c các bi n trong ph m vi toàn c c c)a script
$_SERVER : t*p h p bi n môi trư'ng c)a Web server
$_GET, $_POST : bi n ư c cung c#p các chu i query URL cho script
$_COOKIE : bi n cung c#p HTTP_cookies cho script
$_FILES : bi n cung c#p HTTP POST file uploads cho script
$_ENV : bi n cung c#p môi trư'ng cho script
$_REQUEST : cung c#p các $_GET, $_POST, $_COOKIE
Trang 9$b = $a + $b;
} Sum();
echo $b;
?>
Trang 10echo “<br> out Test a = “ $a;
echo “ in Test a = “ $a;
$a++;
} Test(); // 10 Test(); // 11
?>
Trang 11Ki u d li u ph c h p
m ng
i tư ng
Ki u gi Etc.
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$b = 1.234;
$c = 1.2e3;
$d = 7E-10;
?>
Trang 12}// this is not necessary
if ($show_separators == TRUE) {echo "<hr>\n";
}// because you can simply type
if ($show_separators) {echo "<hr>\n";
echo "He drank some {$beer}s"; // works
$str = 'This is a test.';
$third = $str{2}; // Get the third character of a string
$str = "This is still a test.";
$last = $str{strlen($str)-1}; // Get the last character of a string
$str = 'Look at the sea';
$str{strlen($str)-1} = 'e'; // Modify the last character of a string
?>
Trang 13Ki u
25
Ki u d li u
m ngarray( [key =>] value,
)// key may be an integer or string// value may be any value
Ví d :
<?php
$arr = array("foo" => "bar", 12 => 1);
echo $arr["foo"]; // barecho $arr[12]; // 1
$a = array(5 => 43, 32, 56, "b" => 12);
// this array
$a_n = array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Trang 14$arr[] = 56; // This is the same as $arr[13] = 56;
$arr["x"] = 42; // This adds a new element to the array with key "x"
unset($arr[5]); // This removes the element from the arrayunset($arr); // This deletes the whole array
Trang 15Phép toán
29
Phép toán
Trang 16Phép toán
31
Phép toán
32
Trang 17Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm PHP k t h p v i forms Cookies, SSI (Server side includes), Date PHP-MySQL
else code to be executed if condition is false;
Ví d :
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
else echo "Have a nice day!";
Trang 18code to be executed if expression = label1;
echo "Number 1"; break;
Trang 19Ví d :
<?php
$i=1;
while($i<=5) { echo "The number is " $i "<br />";
Trang 20echo "Hello World!<br />";
$arr=array("one", "two", "three");
foreach ($arr as $value) {
echo "Value: " $value "<br />";
}
?>
Trang 21Hàm nh ngh+a s(n trong PHP
Trang 22echo "Example function.\n";
Trang 23?>
Trang 24return "Making a cup of $type.<br>";
} echo makecoffee();
$string = 'and something extra.';
Trang 25return $num * $num;
} echo square(4); // outputs '16'.
return array (0, 1, 2);
} list ($zero, $one, $two) = small_numbers();
?>
Trang 26PHP k t h p v i forms
Cookies, SSI (Server side includes), Date PHP-MySQL
52
Trang 27<form action="welcome.php" method="POST">
Enter your name: <input type="text" name="name">
Enter your age: <input type="text" name="age">
<input type="submit" value="welcome">
Welcome <?php echo $_POST["name"]; ?>.<br>
You are <?php echo $_POST["age"]; ?> years old!
</body>
</html>
Trang 28Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n Hàm
Trang 29else echo "You are not logged in!<br />";
Trang 31//Prints something like: Monday 15th of January 2003 05:51:38 AMecho date("l dS of F Y h:i:s A");
//Prints something like: Monday the 15thecho date("l \\t\h\e jS");
?>
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n Hàm
PHP k t h p v i forms Cookies, SSI (Server side includes), Date
PHP-MySQL
Trang 3263
MySQL
Download : www.mysql.com , cài "t
Có th cài thêm giao di n qu n tr Ho"c s d ng trình mysql (client)mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor Commands end with ; or \g
Your MySQL connection id is 4 to server version: 5.0.15-ntType 'help;' or '\h' for help Type '\c' to clear the buffer
mysql>
MySQL
64
L nh c b n MySQL
T o xóa c s d li u : create (drop) database dbname
T o xóa ngư'i dùng : create (drop) user uname
T o xóa quy n truy c*p : grant (revoke) …
T o xóa b ng : create (drop) table tname Chèn m,u tin : insert into tname values (…) Xóa m,u tin : delete … from tname where … C*p nh*t : update tname set colname = value …
Trang 3365
MySQL
Trang 34Database changedmysql> CREATE TABLE Person-> (
-> lastname varchar(30),-> firstname varchar(10),-> address varchar(30),-> age int
Chèn các m,u tin vào b ng Person
mysql> insert into Person values ('Thanh-Nghi', 'Do', '84/40, CMT8',31);
mysql> insert into Person values ('Nguyen-Khang', 'Pham', '43/20, Mau Than',27);mysql> insert into Person values ('Nguyen-Binh', 'Le', '12, Nguyen Thong',18);
mysql> insert into Person values ('Trung-Tin', 'Nguyen', '31, Ngo Quyen',12);
mysql> insert into Person values ('Binh-Minh', 'Bui', 'C8, Truong Dinh',22);
mysql>
Trang 35| Nguyen-Khang | Pham | 43/20, Mau Than | 27 |
Trang 36$conn = mysql_connect("127.0.0.1", "nghi", "nghi")
or die("Could not connect: " mysql_error());
$db = mysql_select_db("mydb",$conn)
or die("Could not select database");
$result = mysql_query("SELECT * FROM Person",$conn);
echo "<TABLE BORDER=1>";
echo "<TR><TH> LASTNAME </TH> <TH> FIRSTNAME </TH>
<TH> ADDRESS </TH> <TH> AGE </TH> </TR>";