這次課程將跟大家說明物件導向的封裝、繼承與多形等觀念。
程式設計採視窗程式設計(Windows Form),所以與坊間書籍於終端機環境
設計有很大的不同,但觀念相同。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。
博客來網路書店查詢
星期三, 11月 03, 2010
星期五, 10月 29, 2010
PHP&MySQL--6 2010年10月課程講義預覽
PHP&MySQL--6
一、 新增帳號
二、 PHP連結資料庫語法(db.php,資料庫為pcschool)
1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
三、 mysql編碼與傳輸:unicode.php
四、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】
若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】
若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】
上述兩個練習請修正【修改為mysql_query03.php】:
複製資料表語法
新增資料語法
五、 資料查詢語法
1.基本查詢
2.As的使用
3.排序
六、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】
七、 回傳欄位資料【請依照講義修改mysql_query04.php】
請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】
八、 顯示資料【以array方式設計】 【請依照講義修改mysql_query06.php】
九、 表單送資料至PHP後查詢資料【提供表單網頁】
1. 練習SQL語法中where語法
2. 若直接將post傳送過來的資料送至sql語法?【提供query03.php】
3. 上例修改【請依照講義修改query03.php】
4. 上例修改【請依照講義修改query03.php】
5. 測試各種模糊查詢【提供query04.php,但請修改表單網頁路徑】
十、 傳遞參數給SQL語法:例如查詢customers筆數與範圍(提供表單)
【請自行撰寫】
限制筆數之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〉 |
九、 表單送資料至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
十一、 資料的匯入:mysql
如果將pcschool資料庫刪除呢
十二、 Big5資料的匯入:
十三、 Big5資料的匯出:
十四、 新增帳號
十五、 PHP連結資料庫語法(db.php,資料庫為pcschool)
1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
十六、 mysql編碼與傳輸:unicode.php
十七、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】
若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】
若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】
上述兩個練習請修正【修改為mysql_query03.php】:
複製資料表語法
新增資料語法
十八、 資料查詢語法
1.基本查詢
2.As的使用
3.排序
十九、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】
二十、 回傳欄位資料【請依照講義修改mysql_query04.php】
請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】
二十一、 顯示資料【以array方式設計】
【請依照講義修改mysql_query06.php】
二十二、 其他
一、 資料庫是什麼?
二、 若忘記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. 陣列初始化
2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?
3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?
4. 陣列可用文字做為索引,不過要如何看到全部資料?
二、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?
2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料
3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容
三、 表單與Session:
1. 表單部分(1.php)
2. 接收表單資料
4. 第四頁登出
四、 修改:當網頁關閉時強制刪除session資料
五、 header運用—轉移網頁
〈? header("Location: http://www.google.com"); ?〉
Q1:如果此例的utf-8含有BOM,執行時會如何呢?
Q2:如果網頁上還有其他資訊顯示,網頁可以轉移嗎?
六、 以下幾種函數都必須放在第一行,
且之前不能有任何輸出,包含空白:
header( )、setcookie( )、session_start( )、ob_start( )
七、 header運用—驗證身份後做網頁轉換【提供表單網頁】
八、 header運用—網頁每隔20秒向server讀取資料
九、 header運用—網頁5秒後將轉移至google
十、 與搜尋引擎有關:robots.txt
十二、 關於include
若改為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編碼】
二十一、 談資料表索引
二十二、 其他
一、 陣列簡介
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編碼】
二十一、 談資料表索引
二十二、 其他
訂閱:
文章 (Atom)