溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - 102-2-四技資管二甲乙資料庫管理系統 - 第五週-SELECT AS LIKE PHP 新增至MySQL
 

資訊管理系
副教授
温瑞烘


歷程檔案 Portfolio


關於我 About Me

第五週-SELECT AS LIKE PHP 新增至MySQL

SELECT
1. AS 的應用
   SELECT depart_clASs AS 班級,student_no AS 學號,student_name AS 姓名,
      address AS 地址 FROM stmd;
   select depart_clASs AS 班級,student_no AS 學號,student_name AS 姓名,
      address AS 地址 FROM stmd ORDER BY 學號;
2. LIKE 的應用
   萬用字元 % 表匹配全部  _ 表匹配一個字元
   SELECT * FROM stmd WHERE student_no LIKE "100%";
   SELECT * FROM stmd WHERE student_name LIKE "林%";


作業:使用 LIKE 並以 AS 賦予中文標題
班級代碼的意義如下
第一碼     1:台北校區  2:新竹校區
第二三碼   64:日四技   54:夜四技
第四碼     D:資管系    E:資工系
第五碼     年級
第六碼     1:甲班  2:乙班  3:丙班

1. 查詢班級不是 "164D21" 的所有紀錄
2. 查詢班級不是 "164D21" 的所有紀錄按班級、學號由小到大
3. 查詢學號不是 "10014D001" 的所有紀錄,按學號、班級由小到大
4. 查詢班級是"164D21",學號不是 "10014D001" 的所有紀錄,按學號、班級由小到大
5. 查詢班級是 "164D2"的所有紀錄
6. 查詢班級有 D 的所有紀錄
7. 查詢姓 陳 的所有紀錄,按姓名由小到大
8. 查詢姓名中有 德 的所有紀錄,按學號由大到小
9. 查詢學號中有 D 的所有紀錄,按學號由小到大
10. 查詢學號中有 D 且姓 陳 的所有紀錄,按學號由小到大
11. 查詢台北校區的所有學生,按學號由小到大
12. 查詢新竹校區日四技的所有學生,按學號由小到大
13. 查詢台北校區日四技住台北市的所有學生,按學號由小到大
14. 查詢台北校區日四技不住台北市的所有學生,按學號由小到大
15. 查詢台北校區日四技一年級住台北市的所有學生,按學號由小到大
16. 查詢台北校區日四技一年級甲班住台北市的所有學生,按學號由小到大


<?php
  include "config.php";
  $command=$_POST["command"];                 // 取得命令按鍵
  if ($command=="" || $command=="返回") {     // 首次進入或返回
    display_first_page($conn);                // $conn 為與MySQL連線
  }
  elseif ($command=="查詢") {
    display_search_page($conn);
  }
  elseif ($command=="新增") {
    display_insert_page($conn);
    display_first_page($conn);
  }

  function display_first_page($conn) { // 第一個頁面
    echo "<html><head><title>MySql與PHP結合-查詢範例</title>
          <link rel='stylesheet' type='text/css' href='style.css'>
          </head><body><center>
          <table> <form method='post' action=''>
          <tr class='alt0'><td colspan=4>MySql與PHP結合-查詢-新增-範例</td></tr>
          <tr><td class='alt1'>班級代碼</td>
          <td><input type='text' name='depart_class' /></td>
          <td class='alt1'>學號</td>
          <td><input type='text' name='student_no' /></td></tr>
          <tr><td class='alt1'>學生姓名</td>
          <td><input type='text' name='student_name' /></td>
          <td class='alt1'>地址</td>
          <td><input type='text' name='address' /></td></tr></table>
          <input type='submit' name='command' value='查詢'>
          <input type='submit' name='command' value='新增'>
          </form></center></body></html>";
  }
 
  function display_search_page($conn) {
    $depart_class=trim($_POST["depart_class"]);
    $student_no=trim($_POST["student_no"]);
    $student_name=trim($_POST["student_name"]);
    $address=trim($_POST["address"]);
    if ($depart_class=="") $depart_class="%";   // 查詢全部
    else $depart_class="%".$depart_class."%";   // 查詢匹配
    if ($student_no=="") $student_no="%";       // 查詢全部
    else $student_no="%".$student_no."%";       // 查詢匹配
    if ($student_name=="") $student_name="%";   // 查詢全部
    else $student_name="%".$student_name."%";   // 查詢匹配
    if ($address=="") $address="%";             // 查詢全部
    else $address="%".$address."%";             // 查詢匹配
    // 準備查詢命令按班級學號排序
    $sql="select * from stmd where depart_class like '$depart_class' and ";
    $sql.="student_no like '$student_no' and student_name like '$student_name' ";
    $sql.="and address like '$address' order by depart_class,student_no";
    $result=mysql_query($sql,$conn);            // SQL命令執行
    echo "<html><head><title>MySql與PHP結合-更新-刪除-範例</title>
          <link rel='stylesheet' type='text/css' href='style.css'>
          </head><body><center>
          <table><form method='post' action=''>
          <tr class='alt0'><td colspan=5>MySql與PHP結合-更新-刪除-範例</td></tr>
          <tr class='alt1'><td>班級</td><td>學號</td><td>姓名</td><td>地址</td></tr>";
    $cnt=0;
    while ($myrow=mysql_fetch_array($result)) {  // 取出一列
      $depart_class=$myrow["depart_class"];      // 取出欄位
      $student_no=$myrow["student_no"];          // 取出欄位
      $student_name=$myrow["student_name"];      // 取出欄位
      $address=$myrow["address"];                // 取出欄位
      $bgcolor=$cnt % 2+ 2;
      echo "<tr class='alt$bgcolor'><td>$depart_class</td><td>$student_no</td>
            <td>$student_name</td><td>$address</td></tr>";
      $cnt++;
    }
    echo "</table>
          <input type='submit' name='command' value='返回'>
          </form></center></body></html>";
  }

  function display_insert_page($conn) {
    //將每個攔位資料取出
    if ($depart_class=="" && $student_no=="" && $student_name=="" && $address=="") {
      display_first_page($conn); exit();
    }
    $sql="insert into stmd values('$depart_class','$student_no','$student_name','$address')";
    mysql_query($sql,$conn);                   
  }

  mysql_close($conn);

?>






 

全部共 0則留言
登入帳號密碼代表遵守學術網路規範
 


文章分類 Labels

 


最新文章 Top10

中華科技大學數位化學習歷程 - 意見反應