發佈日期:
分類:
如何在PHP中‧列出MySQL各表格內的所有內容
01. 要達到以上目的,會分為4部份去做。第一為簡單確認,用戶是受權可存取資料;第二是列出指定數據庫所有表格;第三是讓用戶選擇需列出表格名稱;最後是根據用戶選擇,列出表格內容。
02. 程式會分為3部份,第一部份為簡單用戶確認及列出指定數據庫所有表格。建立sql_table_select.php檔案,其中確認密碼是存放於$auth變數內,而所有表格名稱,則直接用mysql_list_tables(“database_name”)取得。
sudo gedit sql_table_select.php
<!-- Content of sql_table_select.php --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>SQL table select</title> <style type="text/css"> .content_text { font-size: 11pt; color: #333; font-family: Arial; } </style> <script type="text/javascript"> function listbox_selectall(listID, isSelect) { var listbox = document.getElementById(listID); for(var count=0; count < listbox.options.length; count++) { listbox.options[count].selected = isSelect; } } function checkbox_select() { if (document.sql_list.sql_all.checked) { listbox_selectall('sql_table', true); } else { listbox_selectall('sql_table', false); } } function checkbox_disable() { document.sql_list.sql_all.checked = false; } </script> </head> <?php $auth = $_POST["auth"]; if (isset($auth) != NULL && $auth == "password") { ?> <body onload="listbox_selectall('sql_table', true);"> <form name="sql_list" id="sql_list" action="sql_table_list.php" method="post"> <p class="content_text"><input name="sql_all" id="sql_all" type="checkbox" onclick="checkbox_select()" checked >Select all table</p> <select name="sql_table[]" id="sql_table" size="20" multiple="true" class="content_text" onchange="checkbox_disable()"> <?php include "db.php"; $result = mysql_list_tables("database_name"); while ($row = mysql_fetch_row($result)) { echo "<option>".$row[0]."</option>"; } ?> </select> <p><input name="submit" id="submit" type="submit" class="content_text" value="View"></p> <form> <?php } else { ?> <body> <form name="sql_list" action="sql_table_select.php" method="post"> <p class="content_text">Authorization code: <input name="auth" id="auth" type="text" class="content_text"></p> <p><input name="submit" id="submit" type="submit" class="content_text" value="Enter"></p> </form> <?php } ?> </body> </html>
03. 建立sql_table_list.php檔案。讓用戶選擇需要的表格名稱後,需要選擇顯示那一個表格的內容,再按相對應連結。
sudo gedit sql_table_list.php
<!-- Content of sql_table_list.php --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>SQL table list</title> <style type="text/css"> .content_text { font-size: 11pt; color: #333; font-family: Arial; } .detail_text { font-size: 10pt; color: #567; font-family: Arial; padding-right: 20px; text-decoration: none; } </style> </head> <body> <?php $submit = $_POST["submit"]; $sql_table = $_POST["sql_table"]; if (isset($submit) != NULL && $submit == 'View') { foreach ($sql_table as $name){ echo '<p class="content_text">'; echo '<a href="sql_table_detail.php?table='.$name.'" class="detail_text">(Detail)</a>'; echo $name; echo '</p>'; } } ?> </body> </html>
04. 建立sql_table_detail.php檔案。當用戶按下連結後,程式會再次連接數據庫,將有關表格內容,在瀏覽器中顯示出來。
sudo gedit sql_table_detail.php
<!-- Content of sql_table_detail.php --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php $table = $_GET["table"]; echo '<title>SQL table detail: '.$table.'</title>'; ?> <style type="text/css"> .table_name { font-size: 14pt; color: #FF8928; font-family: Arial; } .table_text { font-size: 11pt; color: #FFF; background: #08A41A; font-family: Arial; padding: 5px 8px 5px 8px; height: 25px; } .content_text_1 { font-size: 11pt; color: #000; background: #FFFF78; font-family: Arial; padding: 5px 8px 5px 8px; height: 25px; } .content_text_2 { font-size: 11pt; color: #000; background: #78FFF8; font-family: Arial; padding: 5px 8px 5px 8px; height: 25px; } </style> </head> <body> <?php include "db.php"; $sql = "SELECT * FROM ".$table; $result = mysql_query($sql); $table_length = mysql_num_fields($result); echo '<p class="table_name">Table name: '.$table.'</p>'; echo '<table>'; echo '<tr>'; for ($i=0;$i<$table_length;$i++) { echo '<td class="table_text">'; echo mysql_field_name($result, $i); echo '</td>'; } echo '</tr>'; $count = 0; while($row = mysql_fetch_array($result)) { echo '<tr>'; for ($i=0;$i<$table_length;$i++) { if (($count%2) == 0) { echo '<td class="content_text_1">'; } else { echo '<td class="content_text_2">'; } echo $row[$i]; echo '</td>'; } echo '</tr>'; $count++; } echo '</table>'; ?> </body> </html>
05. 建立db.php檔案,以方便連接數據庫時使用。
sudo gedit db.php
<!-- Content of db.php --> <?php mysql_connect("localhost","user","password"); mysql_query("SET NAMES 'utf8'"); mysql_select_db("database_name"); ?>
發佈留言