Advertisement
chenshaoju

PHP-将数据库里某个表的数据全部读出并显示出来。

Dec 19th, 2011
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. //这个PHP脚本的目的是将数据库中的数据全部读出,然后写到页面上,在访问该页面前需要输入密码。
  2. //以DATABASE开头的大写部分为需要按照实际情况修改的部分。
  3. //密码部分的代码来自: http://www.goldapple.name/?action=show&id=737
  4.  
  5. <?php  
  6. $password = "PASSWORD"; // 这里是设置访问密码  
  7. $p = "";  
  8. if(isset($_COOKIE["isview"]) and $_COOKIE["isview"] == $password){  
  9.     $isview = true;  
  10. }else{  
  11.     if(isset($_POST["pwd"])){  
  12.         if($_POST["pwd"] == $password){  
  13.             setcookie("isview",$_POST["pwd"],time()+3600*3);  
  14.             $isview = true;  
  15.         }else{  
  16.             $p = (empty($_POST["pwd"])) ? "需要密码才能查看,请输入密码。" : "密码不正确,请重新输入。";  
  17.         }  
  18.     }else{  
  19.         $isview = false;  
  20.         $p = "内部资料,请联系Chris获取密码。";  
  21.     }  
  22. }  
  23.  
  24. if($isview){ ?>  
  25.  
  26. <?php
  27. //建立数据库链接,
  28.     mysql_connect("localhost", "DATABASEUSERNAME", "DATABASEPASSWORD") or
  29.     die("Could not connect: " . mysql_error());
  30.  
  31. //选择数据库
  32.     mysql_select_db("DATABASENAME");
  33.  
  34. //查询sql语句
  35.     $result = mysql_query("SELECT `DATATIME`, `NAME`, `CONAME`, `EMAIL`, `PHONE`, `SAMESOFT` FROM `DATABASENAME`.`DATABASETABLENAME`");
  36.  
  37.  
  38. //输出查询结果
  39.     echo '
  40. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  41. <html xmlns="http://www.w3.org/1999/xhtml">
  42. <head>
  43. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  44. <title>读取数据库</title>
  45. </head>
  46. <body>
  47. <table width="1020" border="1">
  48. ';
  49.     echo "<tr><td>时间</td><td>姓名</td><td>公司名</td><td>电子邮件</td><td>电话</td><td>备注</td></tr>";
  50.     while ($row = mysql_fetch_array($result)) {
  51.     echo "<tr>";
  52.     echo "<td>",strip_tags($row['DATATIME']) ,"</td>";
  53.     echo "<td>",strip_tags($row['NAME']) ,"</td>";
  54.     echo "<td>",strip_tags($row['CONAME']) ,"</td>";
  55.     echo "<td>",strip_tags($row['EMAIL']) ,"</td>";
  56.     echo "<td>",strip_tags($row['PHONE']) ,"</td>";
  57.     echo "<td>",strip_tags($row['SAMESOFT']) ,"</td>";
  58.     echo "</tr>";
  59.     }
  60. echo "
  61. </table>
  62. </body>
  63. </html>
  64. ";
  65. //释放结果内存
  66.     mysql_free_result($result);
  67. ?>
  68. <?php }else{ ?>  
  69. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  70. <html xmlns="http://www.w3.org/1999/xhtml">  
  71. <head>  
  72. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  73. <meta http-equiv="pragma" content="no-cache" />  
  74. <meta http-equiv="cache-control" content="no-cache" />  
  75. <meta http-equiv="expires" content="0" />  
  76. <title>查看</title>  
  77. <!--[if lt IE 6]>    
  78. <style type="text/css">  
  79. .z3_ie_fix{  
  80.     float:left;  
  81. }  
  82. </style>  
  83. <![endif]-->  
  84. <style type="text/css">  
  85. <!--  
  86. body{  
  87.     background:none;  
  88. }  
  89. .passport{  
  90.     border:1px solid red;  
  91.     background-color:#FFFFCC;  
  92.     width:400px;  
  93.     height:100px;  
  94.     position:absolute;  
  95.     left:49.9%;  
  96.     top:49.9%;  
  97.     margin-left:-200px;  
  98.     margin-top:-55px;  
  99.     font-size:14px;  
  100.     text-align:center;  
  101.     line-height:30px;  
  102.     color:#746A6A;  
  103. }  
  104. -->  
  105. </style>  
  106.         <div class="passport">  
  107.             <div style="padding-top:20px;">  
  108.                 <form action="?yes" method="post" style="margin:0px;">输入查看密码  
  109.                     <input type="password" name="pwd" />  <input type="submit" value="查看" />  
  110.                 </form>  
  111.                 <?php echo $p; ?>  
  112.             </div>  
  113.         </div>  
  114. <?php } ?>  
  115. </body>  
  116. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement