博客來網路書店查詢

書名

博客來網路書店查詢

星期五, 10月 29, 2010

PHP&MySQL--6 2010年10月課程講義預覽

PHP&MySQL--6
一、 新增帳號


grant all privileges on *.* to pcschool@localhost identified by 'phpmysql';

grant all privileges on board.* to php1@localhost identified by 'mysqlstart1';

grant select on board2.* to php3@localhost identified by 'mysqlstart3';



二、 PHP連結資料庫語法(db.php,資料庫為pcschool)


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉資料庫連線〈/title〉〈/head〉〈body〉〈?
$link = @mysql_connect("localhost", "pcschool", "phpmysql");
$link_db = @mysql_select_db("pcschool123446");
if($link) { echo "主機連結OK!....";
if($link_db) { echo "資料庫連結OK!...."; }
if(!$link_db) { echo "資料庫連結失敗!....."; }}
if(!$link) { die( "連結主機失敗!....");}
echo "Hello"; ?〉 〈/body〉〈/html〉



1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
三、 mysql編碼與傳輸:unicode.php


〈?
mysql_query("SET NAMES utf8");
mysql_query("CHARACTER SET utf8");
mysql_query("SET CHARACTER_SET_CLIENT =utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_general_ci");
mysql_query("SET CHARACTER_SET_RESULTS =utf8");
mysql_query("SET CHARACTER_SET_SERVER = utf8");
mysql_query("SET character_set_connection=utf8"); ?〉



四、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉mysql_query〈/title〉〈/head〉〈body〉〈?
mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
//include("unicode.php");
//以下新增語法請做練習
//以下兩行為一行
//$sql = "insert into list (username, email, sex) values('pcschool', 'quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list (email, sex) values('quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list values('quota123@ms14.url.com.tw','pcschool','男')";
$sql = "insert into list values('','pcschool','男')";
mysql_query($sql) or die(mysql_error( ) );
?〉〈/body〉〈/html〉



若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉以下的Query是否有錯?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;
?〉〈/body〉〈/html〉



若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉兩個Query執行會如何?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('er', '23');";
mysql_query($sql);
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;//得到與mysql_error( )
?〉〈/body〉〈/html〉




上述兩個練習請修正【修改為mysql_query03.php】:

$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('$a',$b);";




複製資料表語法


create table insertemployees select * from employees;
create table insertjob select * from job;
create table updateemployees1 select * from employees;
create table updateemployees2 select * from employees;
create table updateemployees3 select * from employees;
create table updatejob select * from job;
create table updateproducts select * from products;
create table delemployees1 select * from employees;
create table delemployees2 select * from employees;
create table delemployees3 select * from employees;
create table deljob select * from job;



新增資料語法


insert into insertjob values (12);

insert into insertjob values ('manager',12);
select * from insertjob;

insert into insertjob values (12,'manager');
select * from insertjob;

insert into employees(lastname,firstname,employeeid) values ('jiannrong','yeh',2009);」
select lastname,firstname,employeeid from employees where employeeid=2009;

insert into insertjob(employeeid,title) select employeeid ,concat(firstname,"-",lastname) from employees;
select * from insertjob



五、 資料查詢語法
1.基本查詢


select * from employees;
select firstname, lastname from employees;
select city from employees;
select distinct city from employees;



2.As的使用


select firstname as f, lastname as l from employees;

select productname, unitprice ,unitsinstock ,unitprice * unitsinstock as total from products;

select firstname,hiredate,curdate( ) as nowdate,(year(curdate( ) )-year(hiredate)) as years from employees;

select firstname,lastname,concat(firstname, "-- ",lastname) as yourname from employees;



3.排序


select firstname, lastname, hiredate from employees order by firstname;
select firstname, lastname, hiredate from employees order by firstname desc;
select firstname, lastname, hiredate from employees order by firstname asc;
select firstname, lastname from employees order by hiredate desc;
select firstname, lastname from employees order by firstname desc,lastname asc;
select firstname, lastname from employees order by lastname asc ,firstname desc;



六、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉計算筆數〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
@mysql_select_db(“pcschool”) or die("無法連結資料庫");
include("unicode.php");
$abc="select username from list";
echo $abc."〈br〉";
$sql = mysql_query($abc) or die(mysql_error( ) );;
$rows = mysql_num_rows($sql);
if($rows==""){
echo "查無資料!";
}else{ echo "有 ".$rows." 筆資料喔!";}
?〉〈/body〉〈/html〉



七、 回傳欄位資料【請依照講義修改mysql_query04.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示記錄〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
echo $sql2;
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID'];
?〉
〈/body〉〈/html〉



請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】

$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";
echo $list1['OrderID']."〈br〉";
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";




八、 顯示資料【以array方式設計】 【請依照講義修改mysql_query06.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示所有資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
while($list1= mysql_fetch_array($sql2))
{
echo $list1['OrderID']." ".$list1['OrderDate']."〈br〉";
}
?〉〈/body〉〈/html〉



九、 表單送資料至PHP後查詢資料【提供表單網頁】
1. 練習SQL語法中where語法

select employeeid,firstname from employees where (employeeid 〉= 3);
select employeeid,firstname from employees where (employeeid in(2,4,9));
select employeeid,firstname from employees where (employeeid not in(2,4,9));
select country,companyname from customers where (country in('Argentina','Mexico'));

select region,companyname from customers where (region is null);
select region,companyname from customers where (region is not null);
select firstname from employees where (firstname like 'a%');
select firstname from employees where (firstname like '%a%');
select firstname from employees where (firstname like '%a');
select companyname,contactname,country from customers where (country in('France','Germany') and companyname like 'b%');

select companyname,contactname,country from customers where (country in('France','Germany') or companyname like 'b%');




2. 若直接將post傳送過來的資料送至sql語法?【提供query03.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



3. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";



4. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
//$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";
$a=$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='$a'";



5. 測試各種模糊查詢【提供query04.php,但請修改表單網頁路徑】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉
〈/head〉〈body〉
〈?php
include("server.php");
//測試各種模糊查詢
$sql="SELECT * FROM orders where CustomerID like '%$a%'";
$sql="SELECT * FROM orders where CustomerID like '$a%'";
$sql="SELECT * FROM orders where CustomerID like '%$a'";
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



十、 傳遞參數給SQL語法:例如查詢customers筆數與範圍(提供表單)
【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉回傳各欄位資料〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
include("unicode.php");
echo $_SERVER['HTTP_REFERER']."〈br〉";
//$sql1="select * from customers limit ".$_POST['first'].",".$_POST['total'];
//$sql1="select * from customers limit $_POST['first'],$_POST['total']";

$a=$_POST['first'];
$b=$_POST['total'];
$sql1="select * from customers limit $a,$b";
//echo $sql1;
//$sql = @mysql_query($sql1);
$sql = mysql_query($sql1) or die(mysql_error());
$rows = @mysql_num_rows($sql);
//echo $rows;
if($rows==""){
echo "查無資料!";
}else{
echo "有 ".$rows." 筆資料喔!";
}
?〉〈/body〉〈/html〉



限制筆數之SQL語法:


select employeeid,firstname, lastname from employees ;
select employeeid,firstname, lastname from employees limit 1,30;
select employeeid,firstname, lastname from employees limit 0,5;
select employeeid,firstname, lastname from employees limit 6;

select productid,productname,unitprice from products order by unitprice desc limit 1;

select employeeid,firstname, lastname from employees order by rand() limit 6;



十一、 其他

星期三, 10月 27, 2010

PHP&MySQL--5 2010年10月課程講義預覽

PHP&MySQL--5
一、 資料庫是什麼?
二、 若忘記root密碼:【請參考網路硬碟提供的指令依序進行密碼設定】
三、 一些MySQL指令
說明 指令
顯示目前資料庫 show databases;
檢視資料表:例如檢視mysql資料庫內的資料表 【請先使用資料庫】 show tables from mysql;
顯示資料表 【請先使用資料庫】 show tables;
檢視欄位 【請先使用資料庫】 show columns from db from mysql;
建立資料庫 create database pcschooldb;
刪除資料庫 drop database pcschooldb;
使用資料庫 use test;
四、 資料庫設計
五、 什麼是SQL檔案
六、 利用phpMyAdmin建pcschool資料庫,將student_create_table.sql匯入
七、 將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
八、 利用phpMyAdmin建立pcschool2資料庫,但校對請選擇「Big5」,並請您將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
九、 談資料表索引
十、 資料的匯出:mysqldump

mysqldump –uroot -pphpmysql pcschool 〉pcschool.sql

mysqldump -uroot -pphpmysql --skip-opt pcschool 〉pcschool2.sql

mysqldump -uroot -pphpmysql pcschool student 〉pcschool3.sql

mysqldump -uroot -pphpmysql --skip-opt pcschool student 〉pcschool4.sql

mysqldump -uroot -pphpmysql pcschool student list 〉pcschool5.sql

mysqldump -uroot -pphpmysql --no-data pcschool 〉pcschool6.sql

mysqldump -uroot -pphpmysql --no-data pcschool list〉pcschool7.sql

mysqldump -uroot -pphpmysql --no-create-info pcschool 〉pcschool8.sql

mysqldump -uroot -pphpmysql --skip-opt --no-create-info pcschool 〉pcschool9.sql

mysqldump -uroot -pphpmysql -B pcschool 〉pcschooldb.sql




十一、 資料的匯入:mysql


mysql -uroot -pphpmysql pcschool〈pcschool.sql
mysql -uroot -pphpmysql counter〈record.sql



如果將pcschool資料庫刪除呢


mysql -uroot -pphpmysql pcschool〈 pcschooldb.sql
mysql -uroot -pphpmysql test〈 pcschooldb.sql



十二、 Big5資料的匯入:


mysql -uroot -pphpmysql --default-character-set=big5 test〈create_list_big5.sql



十三、 Big5資料的匯出:


mysqldump -uroot -pphpmysql --default-character-set=big5 test listbig5 〉listbig53.sql



十四、 新增帳號


grant all privileges on *.* to pcschool@localhost identified by 'phpmysql';

grant all privileges on board.* to php1@localhost identified by 'mysqlstart1';

grant select on board2.* to php3@localhost identified by 'mysqlstart3';



十五、 PHP連結資料庫語法(db.php,資料庫為pcschool)

〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉資料庫連線〈/title〉〈/head〉〈body〉〈?
$link = @mysql_connect("localhost", "pcschool", "phpmysql");
$link_db = @mysql_select_db("pcschool123446");
if($link) { echo "主機連結OK!....";
if($link_db) { echo "資料庫連結OK!...."; }
if(!$link_db) { echo "資料庫連結失敗!....."; }}
if(!$link) { die( "連結主機失敗!....");}
echo "Hello"; ?〉 〈/body〉〈/html〉




1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
十六、 mysql編碼與傳輸:unicode.php


〈?
mysql_query("SET NAMES utf8");
mysql_query("CHARACTER SET utf8");
mysql_query("SET CHARACTER_SET_CLIENT =utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_general_ci");
mysql_query("SET CHARACTER_SET_RESULTS =utf8");
mysql_query("SET CHARACTER_SET_SERVER = utf8");
mysql_query("SET character_set_connection=utf8"); ?〉



十七、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉mysql_query〈/title〉〈/head〉〈body〉〈?
mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
//include("unicode.php");
//以下新增語法請做練習
//以下兩行為一行
//$sql = "insert into list (username, email, sex) values('pcschool', 'quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list (email, sex) values('quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list values('quota123@ms14.url.com.tw','pcschool','男')";
$sql = "insert into list values('','pcschool','男')";
mysql_query($sql) or die(mysql_error( ) );
?〉〈/body〉〈/html〉




若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉以下的Query是否有錯?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;
?〉〈/body〉〈/html〉



若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉兩個Query執行會如何?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('er', '23');";
mysql_query($sql);
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;//得到與mysql_error( )
?〉〈/body〉〈/html〉




上述兩個練習請修正【修改為mysql_query03.php】:

$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('$a',$b);";




複製資料表語法

create table insertemployees select * from employees;
create table insertjob select * from job;
create table updateemployees1 select * from employees;
create table updateemployees2 select * from employees;
create table updateemployees3 select * from employees;
create table updatejob select * from job;
create table updateproducts select * from products;
create table delemployees1 select * from employees;
create table delemployees2 select * from employees;
create table delemployees3 select * from employees;
create table deljob select * from job;




新增資料語法

insert into insertjob values (12);

insert into insertjob values ('manager',12);
select * from insertjob;

insert into insertjob values (12,'manager');
select * from insertjob;

insert into employees(lastname,firstname,employeeid) values ('jiannrong','yeh',2009);」
select lastname,firstname,employeeid from employees where employeeid=2009;

insert into insertjob(employeeid,title) select employeeid ,concat(firstname,"-",lastname) from employees;
select * from insertjob




十八、 資料查詢語法
1.基本查詢


select * from employees;
select firstname, lastname from employees;
select city from employees;
select distinct city from employees;



2.As的使用


select firstname as f, lastname as l from employees;

select productname, unitprice ,unitsinstock ,unitprice * unitsinstock as total from products;

select firstname,hiredate,curdate( ) as nowdate,(year(curdate( ) )-year(hiredate)) as years from employees;

select firstname,lastname,concat(firstname, "-- ",lastname) as yourname from employees;



3.排序


select firstname, lastname, hiredate from employees order by firstname;
select firstname, lastname, hiredate from employees order by firstname desc;
select firstname, lastname, hiredate from employees order by firstname asc;
select firstname, lastname from employees order by hiredate desc;
select firstname, lastname from employees order by firstname desc,lastname asc;
select firstname, lastname from employees order by lastname asc ,firstname desc;



十九、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉計算筆數〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
@mysql_select_db(“pcschool”) or die("無法連結資料庫");
include("unicode.php");
$abc="select username from list";
echo $abc."〈br〉";
$sql = mysql_query($abc) or die(mysql_error( ) );;
$rows = mysql_num_rows($sql);
if($rows==""){
echo "查無資料!";
}else{ echo "有 ".$rows." 筆資料喔!";}
?〉〈/body〉〈/html〉



二十、 回傳欄位資料【請依照講義修改mysql_query04.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示記錄〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
echo $sql2;
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID'];
?〉
〈/body〉〈/html〉



請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】


$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";
echo $list1['OrderID']."〈br〉";
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";



二十一、 顯示資料【以array方式設計】
【請依照講義修改mysql_query06.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示所有資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
while($list1= mysql_fetch_array($sql2))
{
echo $list1['OrderID']." ".$list1['OrderDate']."〈br〉";
}
?〉〈/body〉〈/html〉



二十二、 其他

星期二, 10月 26, 2010

PHP&MySQL--4 2010年10月課程講義預覽

PHP&MySQL--4
一、 陣列簡介
1. 陣列初始化


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列〈/title〉〈/head〉
〈body〉〈?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a〈5; $a++)
echo "$chinese[$a] 〈br〉" ;
?〉〈/body〉〈/html〉



2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列,不給予索引值編號〈/title〉〈/head〉
〈body〉〈?
$chinese[ ] = 80;
$chinese[ ] = 60;
$chinese[ ] = 90;
$chinese[ ] = 50;
$chinese[ ] = 70;
for ($a=0;$a〈5;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立陣列,索引值編號任意給〈/title〉〈/head〉
〈body〉〈?
$chinese=array(
1=〉80,
3=〉60,
6=〉90,
8=〉50,
9=〉70
);
for ($a=0;$a〈=10;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉





4. 陣列可用文字做為索引,不過要如何看到全部資料?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立文字型態索引值陣列〈/title〉〈/head〉
〈body〉〈?
$a=array(
"Jan" =〉 "一月",
"Feb" =〉 "二月",
"Mar" =〉 "三月"
);
echo $a["Mar"]."〈br〉";
?〉〈/body〉〈/html〉



二、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?

for ($a=0;$a〈=10;$a++)
echo $chinese[$a]."〈br〉" ;
foreach ($chinese as $value1)
echo $value1."〈br〉";




2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料


foreach ($a as $value1)
echo $value1."〈br〉";



3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容

foreach ($chinese as $key1 =〉$value1)
echo "座號".$key1."同學的成績為:".$value1."〈br〉" ;




三、 表單與Session:
1. 表單部分(1.php)


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉表單登入〈/title〉〈/head〉〈body〉
〈form name="form1" method="post" action="2.php"〉請輸入姓名:
〈input type="text" name="username" maxlength="6" size="8"〉〈br〉
請輸入密碼:
〈input type="password" name="passwd" maxlength=”6” size="8"〉〈br〉
〈input type="submit"〉〈input type="reset"〉〈/form〉 〈/body〉〈/html〉



2. 接收表單資料

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉驗證表單資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_POST['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if(!isset($_POST['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
if($_POST['username']=="")
{ ?〉
〈script〉
window.alert('請輸入帳號');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if($_POST['passwd']=="")
{
?〉
〈script〉
window.alert('請輸入密碼');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
$_SESSION['username']=$_POST['username'];
$_SESSION['passwd']=$_POST['passwd'];
echo '〈br/〉〈a href="3.php"〉第三頁〈/a〉';
?〉〈/body〉〈/html〉
3. 第三頁的連結
〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉查閱資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_SESSION['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈?
}
if(!isset($_SESSION['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
location.href='./1.php';
〈/script〉
〈?
}
echo '歡迎光臨';
echo '〈br/〉〈a href="4.php"〉登出〈/a〉';
?〉〈/body〉〈/html〉



4. 第四頁登出


〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉登出〈/title〉〈/head〉〈body〉 〈?
session_unset( );
session_destroy( );
?〉
〈script〉
//window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈/body〉〈/html〉



四、 修改:當網頁關閉時強制刪除session資料

〈title〉查閱資料〈/title〉〈/head〉〈body onUnLoad="checkunload();"〉
〈script〉
function checkunload()
{
location.href="./4.php";
}
〈/script〉




五、 header運用—轉移網頁
〈? header("Location: http://www.google.com"); ?〉
Q1:如果此例的utf-8含有BOM,執行時會如何呢?
Q2:如果網頁上還有其他資訊顯示,網頁可以轉移嗎?

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉轉換網址〈/title〉〈/head〉
〈body〉〈?
header("Location: http://www.google.com");
?〉
?〉〈/body〉〈/html〉





六、 以下幾種函數都必須放在第一行,
且之前不能有任何輸出,包含空白:
header( )、setcookie( )、session_start( )、ob_start( )
七、 header運用—驗證身份後做網頁轉換【提供表單網頁】


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉驗證帳號與密碼〈/title〉〈/head〉
〈body〉〈?
if (($_POST['username']=='php') and ($_POST['passwd']=='mysql'))
header("Location: success.php");
else header("Location: failed.php");
?〉〈/body〉〈/html〉






八、 header運用—網頁每隔20秒向server讀取資料



〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁每隔20秒向server讀取資料〈/title〉〈/head〉
〈body〉〈?
header("refresh:20");
echo "現在時間:".date("h:i:s");
?〉〈/body〉〈/html〉



九、 header運用—網頁5秒後將轉移至google



〈? ob_start() ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁5秒後將轉移至google〈/title〉〈/head〉
〈body〉〈?
header('refresh:5; url="http://www.google.com"');
echo "五秒後連結google";
?〉〈/body〉〈/html〉




十、 與搜尋引擎有關:robots.txt


user-agent: *
disallow: /cig-bin
disallow:/members/data
disallow:/*.pdf$
十一、 如果欲禁止google抓取jpg圖片:robots.txt
user-agent: Googlebot-image
disallow:/*.jpg$




十二、 關於include

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉foreach與print_r〈/title〉〈/head〉
〈body〉
〈TABLE align=center border=1 width=80%〉
〈TR〉〈TD〉〈? include("menu.php"); ?〉〈/TD〉
〈TD〉〈P〉這裡請輸入你的內容〈/TD〉〈/TR〉
〈/TABLE〉
〈/body〉〈/html〉





若改為require會有怎樣的情況發生呢?
若將menu.php刪除 會有怎樣的反應呢?
如果不想顯示錯誤訊息,該如何處理呢?
〈TABLE align=center border=1 width=80%〉
〈TR〉〈TD〉〈? if (!@include("menu.php")) die("連結錯誤"); ?〉〈/TD〉
〈TD〉〈P〉這裡請輸入你的內容〈/TD〉〈/TR〉
〈/TABLE〉〈/body〉〈/html〉
十三、 資料庫是什麼?
十四、 若忘記root密碼:【請參考網路硬碟提供的指令依序進行密碼設定】
十五、 一些MySQL指令
說明 指令
顯示目前資料庫 show databases;
檢視資料表:例如檢視mysql資料庫內的資料表 【請先使用資料庫】 show tables from mysql;
顯示資料表 【請先使用資料庫】 show tables;
檢視欄位 【請先使用資料庫】 show columns from db from mysql;
建立資料庫 create database pcschooldb;
刪除資料庫 drop database pcschooldb;
使用資料庫 use test;
十六、 資料庫設計
十七、 什麼是SQL檔案
十八、 利用phpMyAdmin建pcschool資料庫,將student_create_table.sql匯入
十九、 將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
二十、 利用phpMyAdmin建立pcschool2資料庫,但校對請選擇「Big5」,並請您將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
二十一、 談資料表索引
二十二、 其他

星期四, 10月 21, 2010

PHP&MySQL--3 2010年10月課程講義預覽

PHP&MySQL--3
一、 參考書籍:上奇出版的「PHP 6 與MySQL基礎學習教室」
二、 php5的時間調整:date.timezone -〉 Asia/Tokyo或Asia/Taipei
三、 session時間的初始化

〈? //ob_start( );
session_start( ); //啟動 session
ini_set('date.timezone','Asia/Tokyo');
?〉



備註:session暫存路徑修改 ini_set('session.save_path','../');
四、 session的生命週期


〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session的生命週期〈/title〉〈/head〉〈body〉
〈?
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
$_SESSION['a']=5;
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
session_unset( );
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
session_destroy( );
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
?〉 〈a href="session_test2.php"〉session是否可以看見?〈/a〉〈/body〉〈/html〉



說明:變數註冊為session變數與反註冊session變數於PHP6內取消
請檢視若只有執行或執行,第二頁的session變數是否可以顯示內容?

〈? session_start( ); ?〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉無標題文件〈/title〉〈/head〉
〈body〉〈? echo "a:".$_SESSION['a']."〈br〉"; ?〉〈/body〉〈/html〉





五、 單一變數的清除與刪除( session_test3.php)

〈?session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉判斷session類型變數是否存在〈/title〉〈/head〉
〈body〉〈?
if (isset($_SESSION['b']))
{
echo "b存在"."〈br〉";
echo $_SESSION['b']."〈br〉";
}
else
{
echo "b不存在"."〈br〉";
$_SESSION['b']=50;
echo $_SESSION['b']."〈br〉";
}
?〉
〈a href="session_test4.php"〉session是否可以看見?〈/a〉〈/body〉〈/html〉






session_test4.php


〈? session_start( ); ?〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉變數清除與刪除〈/title〉〈/head〉〈body〉
〈?
echo "1b:".$_SESSION['b']."〈br〉";
//$_SESSION['b']="";
unset($_SESSION['b']);
echo "2b:".$_SESSION['b']."〈br〉";
?〉〈a href="session_test3.php"〉返回 〈/a〉〈/body〉〈/html〉




六、 網頁人數統計,加上session限制

〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉session只能使用一次〈/title〉〈/head〉〈body〉
〈?
session_start( );
if(!isset($_SESSION['counter']))
{
$_SESSION['counter']=1;
echo "歡迎光臨";
}
else
echo "已登錄過請勿重複登入";
?〉〈/body〉〈/html〉





七、 表單與Session:
1. 表單部分(1.php)

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉表單登入〈/title〉〈/head〉〈body〉
〈form name="form1" method="post" action="2.php"〉請輸入姓名:
〈input type="text" name="username" maxlength="6" size="8"〉〈br〉
請輸入密碼:
〈input type="password" name="passwd" maxlength=”6” size="8"〉〈br〉
〈input type="submit"〉〈input type="reset"〉〈/form〉 〈/body〉〈/html〉




2. 接收表單資料

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉驗證表單資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_POST['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if(!isset($_POST['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
if($_POST['username']=="")
{ ?〉
〈script〉
window.alert('請輸入帳號');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if($_POST['passwd']=="")
{
?〉
〈script〉
window.alert('請輸入密碼');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
$_SESSION['username']=$_POST['username'];
$_SESSION['passwd']=$_POST['passwd'];
echo '〈br/〉〈a href="3.php"〉第三頁〈/a〉';
?〉〈/body〉〈/html〉




3. 第三頁的連結

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉查閱資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_SESSION['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈?
}
if(!isset($_SESSION['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
location.href='./1.php';
〈/script〉
〈?
}
echo '歡迎光臨';
echo '〈br/〉〈a href="4.php"〉登出〈/a〉';
?〉〈/body〉〈/html〉




4. 第四頁登出




〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉登出〈/title〉〈/head〉〈body〉 〈?
session_unset( );
session_destroy( );
?〉
〈script〉
//window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈/body〉〈/html〉




八、 Cookie基本練習
1. 儲存Cookie


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉儲存Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "php", time( )+1800);//1800代表1800秒
?〉〈/body〉〈/html〉



2. 顯示Cookie


〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉顯示Cookie〈/title〉〈/head〉
〈body〉〈?
if (isset($_COOKIE["a"]))
echo "cookie內容為".$_COOKIE["a"];
else
echo "沒有資料";
?〉〈/body〉〈/html〉



3. 刪除Cookie(時間到期)

〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉刪除Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "php", time( )-1800);
?〉〈/body〉〈/html〉




4. 刪除Cookie(變數沒有內容)

〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉儲存Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "", time( )+1800);
?〉〈/body〉〈/html〉




九、 Cookie綜合練習
1. 設定Cookie,檔名為showcookie1.php

〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習〈/title〉〈/head〉〈body〉
〈?$tomorrow = mktime (0,0,0,date("m"),date("d")+1,date("Y")); //mktime
$nextmonth = mktime (0,0,0,date("m")+1,date("d"),date("Y"));
$nextyear = mktime (0,0,0,date("m"),date("d"),date("Y")+1);
$yourname="第6個cookie,Cookie變數的內容由變數取得資料";
$testtime=mktime(0,0,0,3,13,2009);
setcookie ("a[0]","設定保存1800秒", time( )+1800);
setcookie ("a[1]","設定保存至2009年3月13日",$testtime);
setcookie ("a[2]","設定保存至明天",$tomorrow);
setcookie ("a[3]","設定保存至下一個月",$nextmonth);
setcookie ("a[4]","設定保存至明年",$nextyear);
setcookie("a[5]","$yourname",time( )+1800);
echo '〈a href="showcookie2.php"〉查詢Cookies〈/a〉';
?〉〈/body〉〈/html〉





2. 查詢Cookie,檔名為showcookie2.php


〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習查詢〈/title〉〈/head〉〈body〉〈?
$value1=$_COOKIE["a"];
for ($for1=0;$for1〈5;$for1++)
{ $a=$for1+1;
echo "第".$a."的內容為".$value1[$for1]."〈br〉";
}
echo '〈a href="showcookie1php"〉重新建立Cookies〈/a〉〈br〉';
echo '〈a href="showcookie3.php"〉刪除幾個Cookies〈/a〉〈br〉';
?〉〈/body〉〈/html〉



3. 刪除Cookie:檔名為showcookie3.php


〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習刪除〈/title〉〈/head〉〈body〉〈?
echo "設定保存1800秒,現在減1800秒"."〈br〉";
echo "設定保存至2009年3月13日,現在內容沒了"."〈br〉";
setcookie ("a[0]","",time( )-1800);//內容沒了
setcookie ("a[1]","");//內容沒了
echo '〈a href="showcookie1php"〉重新建立Cookies〈/a〉〈br〉';
echo '〈a href="showcookie3.php"〉刪除幾個Cookies〈/a〉〈br〉';
?〉〈/body〉〈/html〉




十、 header運用—轉移網頁
〈? header("Location: http://www.google.com"); ?〉
Q1:如果此例的utf-8含有BOM,執行時會如何呢?
Q2:如果網頁上還有其他資訊顯示,網頁可以轉移嗎?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉轉換網址〈/title〉〈/head〉
〈body〉〈?
header("Location: http://www.google.com");

?〉
?〉〈/body〉〈/html〉



十一、 以下幾種函數都必須放在第一行,
且之前不能有任何輸出,包含空白:
header( )、setcookie( )、session_start( )、ob_start( )
十二、 header運用—驗證身份後做網頁轉換【提供表單網頁】



〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉驗證帳號與密碼〈/title〉〈/head〉
〈body〉〈?
if (($_POST['username']=='php') and ($_POST['passwd']=='mysql'))
header("Location: success.php");
else header("Location: failed.php");
?〉〈/body〉〈/html〉




十三、 header運用—網頁每隔20秒向server讀取資料


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁每隔20秒向server讀取資料〈/title〉〈/head〉
〈body〉〈?
header("refresh:20");
echo "現在時間:".date("h:i:s");
?〉〈/body〉〈/html〉



十四、 其他

星期一, 10月 18, 2010

PHP&MySQL--2 2010年10月課程講義預覽 修改

PHP&MySQL--2
一、 表單與資料接收的練習
1. text與password 表單【form1.html】:

〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form1" method="post" action="1.php" 〉
username:〈input type="text" name="username" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉




接收:


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉〈/head〉〈body〉
〈?
echo "1:".$_POST['username']."〈br〉";
echo "2:".$_POST['passwd']."〈br〉";
echo "3:".$username."〈br〉";
echo "4:".$passwd."〈br〉";
echo "5:".$_GET['username']."〈br〉";
echo "6:".$_GET['passwd']."〈br〉";
?〉
〈/body〉〈/html〉



2. 選擇鈕
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="3.php" 〉
sex:
〈input type="radio" name="sex" value="boy"〉公的
〈input type="radio" name="sex" value="girl"〉母的
〈br〉
Blood:
〈input type="radio" name="blood" value="O"〉O
〈input type="radio" name="blood" value="A"〉A
〈input type="radio" name="blood" value="B"〉B
〈input type="radio" name="blood" value="AB" checked 〉AB
〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉



接收:


〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
sex is 〈? echo $_GET['sex'];?〉〈br〉
blood is 〈? echo $_GET['blood'];?〉〈br〉
〈/body〉
〈/html〉




checked與disabled屬性的設定
3. 核選框
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="4.php" 〉
〈input type="checkbox" name="a" value="a"〉選項1
〈input type="checkbox" name="b" value="b"〉選項2
〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉



接收:


〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
a is 〈? echo $_GET['a'];?〉〈br〉
b is 〈? echo $_GET['b'];?〉〈br〉
〈/body〉
〈/html〉



4. 下拉式選單
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form6" method="get" action="6.php" 〉
please select:
〈select name="travel" size="1"〉
〈option value="1"〉台北烏來〈/option〉
〈option〉台北101〈/option〉
〈option〉台北林家花園〈/option〉
〈option value="4" selected〉D〈/option〉
〈option value="5"〉E〈/option〉
〈option value="6"〉F〈/option〉
〈option value="7"〉G〈/option〉
〈option value="8"〉H〈/option〉
〈option value="9"〉I〈/option〉
〈/select〉〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈/form〉
〈/body〉
〈/html〉



selected與disabled屬性的設定
接收:


〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=UTF-8"〉
〈title〉request select〈/title〉〈/head〉
〈body〉
〈? echo $_GET['travel'];?〉
〈/body〉〈/html〉



二、 表單傳遞的問題:表單資料必須透過網址列才可傳送到第三頁
1. 表單網頁【請將form1.html改存成form2.html】


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="form1.php" 〉
username:〈input type="text" name="username" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉



2. 接收資料,並在下方加入連結【請將1.php改存成form1.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉〈/head〉
〈body〉〈?
echo "1:".$_POST['username']."〈br〉";
echo "2:".$_POST['passwd']."〈br〉";
$myname=$_POST['username'];
if($_POST['username']=="") die("沒有帳號");
?〉〈a href="form2.php"〉123〈/a〉
〈a href="form2.php?username=〈? echo $myname; ?〉"〉123〈/a〉
〈/body〉
〈/html〉



3. form2.php如果不經form1.php的情況下可以顯示資料嗎?

〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
是否有登入帳號的資訊?
〈?
echo "post方式傳送的帳號:".$_POST['username']."〈br〉"; ?〉
透過網址列傳送帳號,具有危險性:
〈?
echo "網址列傳送的帳號:".$_GET['username']."〈br〉"; ?〉
〈/body〉
〈/html〉




三、 表單傳遞的問題:使用者可在本機儲存表單html送至Server


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="http://localhost/class1/form1.php" 〉
username:〈input type="text" name="username" value="haha" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10" value="cc"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉



四、 介紹checkbox分組之前,先介紹陣列
1. 陣列初始化

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列〈/title〉〈/head〉
〈body〉〈?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a〈5; $a++)
echo "$chinese[$a] 〈br〉" ;
?〉〈/body〉〈/html〉




2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列,不給予索引值編號〈/title〉〈/head〉
〈body〉〈?
$chinese[ ] = 80;
$chinese[ ] = 60;
$chinese[ ] = 90;
$chinese[ ] = 50;
$chinese[ ] = 70;
for ($a=0;$a〈5;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立陣列,索引值編號任意給〈/title〉〈/head〉
〈body〉〈?
$chinese=array(
1=〉80,
3=〉60,
6=〉90,
8=〉50,
9=〉70
);
for ($a=0;$a〈=10;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



4. 陣列可用文字做為索引,不過要如何看到全部資料?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立文字型態索引值陣列〈/title〉〈/head〉
〈body〉〈?
$a=array(
"Jan" =〉 "一月",
"Feb" =〉 "二月",
"Mar" =〉 "三月"
);
echo $a["Mar"]."〈br〉";
?〉〈/body〉〈/html〉



五、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?

for ($a=0;$a〈=10;$a++)
echo $chinese[$a]."〈br〉" ; foreach ($chinese as $value1)
echo $value1."〈br〉";




2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料


foreach ($a as $value1)
echo $value1."〈br〉";



3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容


foreach ($chinese as $key1 =〉$value1)
echo "座號".$key1."同學的成績為:".$value1."〈br〉" ;



六、 session start():檔名為session1.php

〈? session_start(); ?〉
〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session start()〈/title〉〈/head〉〈body〉 〈?
echo '〉〉第一頁〈〈';
$_SESSION['color'] = '漂亮';
$_SESSION['city'] = '台北';
$_SESSION['time'] = date("Y年m月d日 H時i分s秒");
echo '〈br /〉〈a href="session2.php"〉第二頁〈/a〉';
?〉〈/body〉〈/html〉




檔名為session2.php


〈? ob_start();//output buffering on
session_start(); //啟動 session
?〉
〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session start() page2〈/title〉〈/head〉〈body〉 〈?
echo '〉〉第二頁〈〈 〈br /〉';
echo $_SESSION['color']."〈br〉";
echo $_SESSION['city']."〈br〉";
echo date("Y年m月d日 H時i分s秒")." ==〉 ".$_SESSION['time'];
echo '〈br/〉〈a href="session1.php"〉第一頁〈/a〉';
?〉〈/body〉〈/html〉



七、 php5的時間調整:date.timezone -〉 Asia/Tokyo或Asia/Taipei
八、 session時間的初始化


〈? //ob_start();
session_start(); //啟動 session
ini_set('date.timezone','Asia/Tokyo');
?〉



備註:session暫存路徑修改 ini_set('session.save_path','../');
九、 其他

星期五, 10月 15, 2010

這幾個月我就不會將課表貼在部落格上

最近趕著寫第二本書,所以課表並未在部落格上公布,巨匠學員若對我的課程有興趣,
可與台北東區認證中心聯絡與詢問,這幾個月我就不會將課表貼在部落格上,謝謝!

PHP&MySQL--2 2010年10月課程講義預覽

PHP&MySQL--2
一、 表單與資料接收的練習
1. text與password 表單【form1.html】:
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form1" method="post" action="1.php"〉
username:〈input type="text" name="username" size="6"〉〈br /〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br /〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉
接收:
〈html〉〈head〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉〈/head〉〈body〉
〈?
echo "1:".$_POST['username']."〈br /〉";
echo "2:".$_POST['passwd']."〈br /〉";
echo "3:".$username."〈br /〉";
echo "4:".$passwd."〈br /〉";
echo "5:".$_GET['username']."〈br /〉";
echo "6:".$_GET['passwd']."〈br /〉";
?〉
〈/body〉〈/html〉
2. 選擇鈕
表單:
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="3.php"〉
sex:
〈input type="radio" name="sex" value="boy"〉公的
〈input type="radio" name="sex" value="girl"〉母的
〈br /〉
Blood:
〈input type="radio" name="blood" value="O"〉O
〈input type="radio" name="blood" value="A"〉A
〈input type="radio" name="blood" value="B"〉B
〈input type="radio" name="blood" value="AB"〉AB
〈br /〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉
接收:
〈html〉
〈head〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
sex is 〈? echo $_GET['sex'];?〉〈br /〉
blood is 〈? echo $_GET['blood'];?〉〈br /〉
〈/body〉
〈/html〉
checked與disabled屬性的設定
3. 核選框
表單:
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="4.php"〉
〈input type="checkbox" name="a" value="a"〉選項1
〈input type="checkbox" name="b" value="b"〉選項2
〈br /〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉
接收:
〈html〉
〈head〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
a is 〈? echo $_GET['a'];?〉〈br /〉
b is 〈? echo $_GET['b'];?〉〈br /〉
〈/body〉
〈/html〉
4. 下拉式選單
表單:
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form6" method="get" action="6.php"〉
please select:
〈select name="travel" size="1"〉
〈option value="1"〉台北烏來〈/option〉
〈option〉台北101〈/option〉
〈option〉台北林家花園〈/option〉
〈option value="4"〉D〈/option〉
〈option value="5"〉E〈/option〉
〈option value="6"〉F〈/option〉
〈option value="7"〉G〈/option〉
〈option value="8"〉H〈/option〉
〈option value="9"〉I〈/option〉
〈/select〉〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈/form〉
〈/body〉
〈/html〉
selected與disabled屬性的設定
接收:
〈html〉〈head〉〈meta equiv="content-type" content="text/html;charset=UTF-8"〉
〈title〉request select〈/title〉〈/head〉
〈body〉
〈? echo $_GET['travel'];?〉
〈/body〉〈/html〉
二、 表單傳遞的問題:表單資料必須透過網址列才可傳送到第三頁
1. 表單網頁【請將form1.html改存成form2.html】
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="form1.php"〉
username:〈input type="text" name="username" size="6"〉〈br /〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br /〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉
2. 接收資料,並在下方加入連結【請將1.php改存成form1.php】
〈html〉
〈head〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
〈?
// put your code here
echo "1:".$_POST['username']."〈br /〉";
echo "2:".$_POST['passwd']."〈br /〉";
$myname=$_POST['username'];
if($_POST['username']=="") die("沒有帳號");
?〉
〈a href="form2.php"〉123〈/a〉
〈a href="" username="〈?"〉"〉123〈/a〉
〈/body〉
〈/html〉
3. form2.php如果不經form1.php的情況下可以顯示資料嗎?
〈html〉
〈head〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
是否有登入帳號的資訊?
〈?
echo "post方式傳送的帳號:".$_POST['username']."〈br /〉"; ?〉
透過網址列傳送帳號,具有危險性:
〈?
echo "網址列傳送的帳號:".$_GET['username']."〈br /〉"; ?〉
〈/body〉
〈/html〉
三、 表單傳遞的問題:使用者可在本機儲存表單html送至Server
〈html〉
〈head〉
〈title〉〈/title〉
〈meta equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="http://localhost/class1/form1.php"〉
username:〈input type="text" name="username" value="haha" size="6"〉〈br /〉
password:〈input type="password" name="passwd" maxlength="6" size="10" value="cc"〉〈br /〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉
四、 回顧與思考:
1. 如何做條件判斷:請參考網路硬碟上所提供的表單與接收資料檔案,並確認這樣的條件判斷是否正確?
2. 如何判斷是否有接收表單資料:請參考網路硬碟上所提供的表單與接收資料檔案,並確認若沒經過表單執行會如何?另請思考若經過表單但沒有輸入資料又會如何?
3. for迴圈會如何執行:請參考網路硬碟上所提供的PHP檔案,並確認若條件改為以下的條件,PHP會如何執行?
for ($i=1;$i〈=10;$i--) for ($i=1;$i!=10;$i+=2)
for ($i=1;$i〉=10;$i++)
4. 另外兩種迴圈的特色:請參考網路硬碟上所提供的PHP檔案
5. 迴圈如何中斷:請參考網路硬碟上所提供的PHP檔案分別進行修改,並請觀察執行結果。
〈html〉〈head〉〈meta equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉exit、break、continue...中斷指令〈/title〉〈/head〉〈body〉
〈?
for ($i=1; $i〈=10; $i++){
if($i==5){
echo "迴圈停止〈br /〉";
exit;
}
$a = $a+$i;
} 〈?
for ($i=1; $i〈=10; $i++){
if($i==5){
echo "迴圈停止〈br /〉";
break;
}
$a = $a+$i;
} 〈?
for ($i=1; $i〈=10; $i++){
if($i==5){
echo '$i=5迴圈停止〈br /〉';
continue;
}
$a = $a+$i;
}
echo "總和是:";
echo $a; ?〉〈/body〉〈/html〉
五、 介紹checkbox分組之前,先介紹陣列
1. 陣列初始化
〈html〉〈head〉
〈meta equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列〈/title〉〈/head〉
〈body〉〈?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a〈5; $a++)
echo "$chinese[$a] 〈br /〉" ;
?〉〈/body〉〈/html〉
2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?
〈html〉〈head〉
〈meta equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列,不給予索引值編號〈/title〉〈/head〉
〈body〉〈?
$chinese[ ] = 80;
$chinese[ ] = 60;
$chinese[ ] = 90;
$chinese[ ] = 50;
$chinese[ ] = 70;
for ($a=0;$a〈5;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br /〉" ;
?〉〈/body〉〈/html〉
3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?
〈html〉〈head〉
〈meta equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立陣列,索引值編號任意給〈/title〉〈/head〉
〈body〉〈?
$chinese=array(
1=〉80,
3=〉60,
6=〉90,
8=〉50,
9=〉70
);
for ($a=0;$a〈=10;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br /〉" ;
?〉〈/body〉〈/html〉
4. 陣列可用文字做為索引,不過要如何看到全部資料?
〈html〉〈head〉
〈meta equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立文字型態索引值陣列〈/title〉〈/head〉
〈body〉〈?
$a=array(
"Jan" =〉 "一月",
"Feb" =〉 "二月",
"Mar" =〉 "三月"
);
echo $a["Mar"]."〈br /〉";
?〉〈/body〉〈/html〉
六、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?
for ($a=0;$a〈=10;$a++)
echo $chinese[$a]."〈br /〉" ; foreach ($chinese as $value1)
echo $value1."〈br /〉";
2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料
foreach ($a as $value1)
echo $value1."〈br /〉";
3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容
foreach ($chinese as $key1 =〉$value1)
echo "座號".$key1."同學的成績為:".$value1."〈br /〉" ;
七、 其他