![]() |
溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - 103-2-資管二甲乙資料庫管理系統 - 第2週-MYSQL-PHP-SELECT |
| 第2週-MYSQL-PHP-SELECTMySQL與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. search_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_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> <td class='alt1'><input type='submit' name='command' value='查詢'></td></tr> </table></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>"; } ?> 作業: 擴充前述程式可查詢顯示所有欄位的資料
|
|
中華科技大學數位化學習歷程 - 意見反應 | ![]() |