溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - 104-1-資料庫程式設計-基礎 - 第16週-MySQL-PHP-SELECT
 

資訊管理系
副教授
温瑞烘


歷程檔案 Portfolio


關於我 About Me

第16週-MySQL-PHP-SELECT

 
MySQL與PHP的結合
 
1. config.php 儲存為utf8  儲存在 www下
 
<?php
  $host = "localhost";      // 本機
  $user = "root";           // username
  $pwd = "1234";            // password
  $dbname="mydb";           // 資料庫名稱
  $conn=mysqli_connect($host,$user,$pwd) or die(mysqli_connect_error()); // 連接資料庫
  mysqli_query($conn,'SET NAMES utf8');                               // 查詢結果utf8
  mysqli_select_db($conn,$dbname) or die(mysqli_error($conn));        //使用資料庫
?>
 
2. style.css 儲存為utf8  放在 www下
 
 
 <style type='text/css'>
 
   body { width:100%; margin:0 padding:0; font-family:標楷體; }
   table { border-collapse:collapse; border:1px solid black;
           empty-cells:show; width:100%; }
   th { background-color:ccffff; border: 1px solid; font-family:標楷體;}
   td { border: 1px solid; text-align : center; font-family:標楷體; }
   .alt0 { background-color:#00ffff; font-weight:bold; font-family:標楷體; }
   .alt1 { background-color:#ccccff; font-family:標楷體; }
   .alt2 { background-color:#add8e6; font-family:標楷體; }
   .alt3 { background-color:#fff8c6; font-family:標楷體; }
   a: { background:ccccff; border:1px solid ccc; color:000;
       padding:.3em .5em;  margin-top:1px;  margin-bottom:1px; text-align:center;
       text-decoration:none; display: inline-block; font-family:標楷體; }
   a:link {background-color:ff88ee } 
   a:visited {background-color:FFFF85;}
   a:hover {background-color:FF704D; }  
   a:active {background-color:FF704D; } 
   p {background-color: #81F781; display:inline;font-family:標楷體; font-weight:bold;}  
   .cmd  {font-family:標楷體; font-size:18px; background-color:ccccff; width:6em; } 
   .cmd1 {font-family:標楷體; font-size:18px; background-color:ccccff; width:10em; } 
 </style>
 
 
 
3. stmd.php  儲存為utf8 儲存在 www下
  
<?php
  include "config.php";
  if (isset($_POST["command"])) $command=$_POST["command"];
  else $command="";
 
  if ($command=="") {
    display_first_page($conn);
  }
  elseif ($command=="新增") {
    display_insert_page($conn);
    display_first_page($conn);
  }
  elseif ($command=="查詢") {
    display_search_page($conn);
  }
  mysqli_close($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>
          <form method='post' action=''>
          <table><tr><td class='alt1'>學號</td>
          <td><input type='text' name='student_no' size='9'></td></tr></table>
          <input class='cmd' type='submit' name='command' value='查詢'>
          <input class='cmd' type='submit' name='command' value='新增'>
          </form></center></body></html>";
  }
 
  function display_search_page($conn) {
    $student_no=trim($_POST["student_no"]); // 以學號為鍵值查詢
    if ($student_no=="") $student_no="%";   // 查詢全部
    else $student_no="%".$student_no."%";
    $sql="select * from stmd where student_no like '$student_no' order by student_no";
    $result=mysqli_query($conn,$sql);        // SQL命令執行
    echo "<html><head><title>MySql與PHP結合-查詢範例</title>
          <link rel='stylesheet' type='text/css' href='style.css'>
          </head><body><center>
          <table><tr class='alt1'><td>班級</td><td>學號</td></tr>";
    $cnt=0;
    while ($myrow=mysqli_fetch_array($result)) {  // 取出一列
      $depart_class=$myrow["depart_class"];      // 取出欄位
      $student_no=$myrow["student_no"];          // 取出欄位
      $bgc=($cnt % 2) + 2;
      echo "<tr class='alt$bgc'><td>$depart_class</td><td>$student_no</td></tr>";
      $cnt++;
    }
    echo "</table></center></body></html>"; 
  }
 
  function display_insert_page($conn) {
    $student_no=trim($_POST["student_no"]); 
    if ($student_no=="") {
      echo "學號必須輸入,請回前頁重新輸入"; die();
    }
    $sql="insert into stmd (student_no) values('$student_no')";
    mysqli_query($conn,$sql); 
    echo "<script> alert('已經插入一筆資料'); </script>";
  }
 
?>
 
 
 
?>
 
作業: 擴充前述程式可查詢顯示所有欄位的資料
 
 
全部共 0則留言
登入帳號密碼代表遵守學術網路規範
 


文章分類 Labels

 


最新文章 Top10

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