2015-05-11 28 views
-2

我試圖將數據庫的內容查看到網頁中。我使用這個代碼:在查看網頁中的數據庫內容時需要一點幫助

<?php 
error_reporting(0); 
$host="localhost"; 
$username="root"; 
$password=""; 
$database="pncollege"; 

mysql_connect($host,$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 
$query="SELECT * FROM data"; 
$result=mysql_query($query); 

$num=mysql_numrows($result); 

mysql_close(); 

echo ""; 

$i=0; 
while ($i < $num) { 

$email=mysql_result($result,$i,"email"); 
$name=mysql_result($result,$i,"name"); 


echo ""; 

$i++; 
} 

?> 
<!DOCTYPE HTML> 
<html> 
<head> 
<title>Free Guidance Website Template | Programs :: w3layouts</title> 
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" /> 
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link href='http://fonts.googleapis.com/css?family=Montserrat+Alternates' rel='stylesheet' type='text/css'> 
<!------ Light Box ------> 
<script src="js/jquery.min.js"></script> 
<link rel="stylesheet" href="css/swipebox.css"> 
<script src="js/ios-orientationchange-fix.js"></script> 
<script src="js/jquery.swipebox.min.js"></script> 
<script type="text/javascript"> 
     jQuery(function($) { 
      $(".swipebox").swipebox(); 
     }); 
    </script> 
    <style> 

    /* 
    Max width before this PARTICULAR table gets nasty 
    This query will take effect for any screen smaller than 760px 
    and also iPads specifically. 
    */ 
    @media 
    only screen and (max-width: 760px), 
    (min-device-width: 768px) and (max-device-width: 1024px) { 

     /* Force table to not be like tables anymore */ 
     table, thead, tbody, th, td, tr { 
      display: block; 
     } 

     /* Hide table headers (but not display: none;, for accessibility) */ 
     thead tr { 
      position: absolute; 
      top: -9999px; 
      left: -9999px; 
     } 

     tr { border: 1px solid #ccc; } 

     td { 
      /* Behave like a "row" */ 
      border: none; 
      border-bottom: 1px solid #eee; 
      position: relative; 
      padding-left: 50%; 
     } 

     td:before { 
      /* Now like a table header */ 
      position: absolute; 
      /* Top/left values mimic padding */ 
      top: 6px; 
      left: 6px; 
      width: 45%; 
      padding-right: 10px; 
      white-space: nowrap; 
     } 

     /* 
     Label the data 
     */ 
     td:nth-of-type(1):before { content: "First Name"; } 
     td:nth-of-type(2):before { content: "Last Name"; } 
     td:nth-of-type(3):before { content: "Job Title"; } 
     td:nth-of-type(4):before { content: "Favorite Color"; } 
     td:nth-of-type(5):before { content: "Wars of Trek?"; } 
     td:nth-of-type(6):before { content: "Porn Name"; } 
     td:nth-of-type(7):before { content: "Date of Birth"; } 
     td:nth-of-type(8):before { content: "Dream Vacation City"; } 
     td:nth-of-type(9):before { content: "GPA"; } 
     td:nth-of-type(10):before { content: "Arbitrary Data"; } 
    } 

    /* Smartphones (portrait and landscape) ----------- */ 
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 480px) { 
     body { 
      padding: 0; 
      margin: 0; 
      width: 320px; } 
     } 

    /* iPads (portrait and landscape) ----------- */ 
    @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { 
     body { 
      width: 495px; 
     } 
    } 
* { 
    margin: 0; 
    padding: 0; 
} 
body { 
    font: 14px/1.4 Georgia, Serif; 
} 
#page-wrap { 
    margin: 50px; 
} 
p { 
    margin: 20px 0; 
} 

    /* 
    Generic Styling, for Desktops/Laptops 
    */ 
    table { 
     width: 100%; 
     border-collapse: collapse; 
    } 
    /* Zebra striping */ 
    tr:nth-of-type(odd) { 
     background: #eee; 
    } 
    th { 
     background: #333; 
     color: white; 
     font-weight: bold; 
    } 
    td, th { 
     padding: 6px; 
     border: 1px solid #ccc; 
     text-align: left; 
    } 
    </style> 
    <!------ Eng Light Box ------> 
</head> 
<body> 
<div class="header-bg"> 
    <div class="wrap"> 
     <div class="total-box"> 
       <div class="total"> 
        <div class="header_top"> 
         <div class="menu"> 
          <ul> 
           <li><a href="index.html">Home</a></li> 
           <li><a href="about.html">About</a></li> 
           <li><a href="faculty.html">Faculties</a></li> 
           <li><a href="picture.html">Picture Gallery</a></li> 
           <li class="active"><a href="principal.html">Principal's Desk</a></li> 
           <li><a href="contact.html">Contact</a></li> 
           <div class="clear"></div> 
          </ul> 
         </div> 
         <ul class="follow_icon"> 
          <li><a href="#" style="opacity: 1;"><img src="images/fb.png" alt=""></a></li> 
          <li><a href="#" style="opacity: 1;"><img src="images/tw.png" alt=""></a></li> 
          <li><a href="#" style="opacity: 1;"><img src="images/rss.png" alt=""></a></li> 
         </ul> 
         <div class="clear"></div> 
       </div> 
       <div class="header-bottom"> 
        <div class="logo"> 
         <img src="images/logo.png"> 
        </div> 
        <div class="logo"> 
         <h1><a href="index.html">P.N. College, Parsa</a></h1> 
         <h2><a href="index.html">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(A constituent unit of Jaiprakash University)</a></h2> 
         </div> 
        <div class="search"> 
          <form> 
           <input type="text" value=""> 
           <input type="submit" value=""> 
          </form> 
        </div> 
        <div class="clear"></div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
    <div class="banner-box"> 
     <div class="wrap"> 
      <div class="main-top"> 
       <div class="main"> 
       <div class="heading3"> 
        <h3 style="text-align:center">Admin Panel</h3> 
        <hr><br> 
       </div> 
       <div class="section group"> 

<center> 
     <table> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Email</th> 
        <th>Message</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td><?php echo "$userName"; ?></td> 
        <td><?php echo "$userEmail"; ?></td> 
        <td><?php echo "$userMsg"; ?></td> 
       </tr> 
      </tbody> 
     </table> 
</center> 


        <div class="clear"></div> 
       </div> 

       </div> 
      </div> 
     </div> 


    </div> 
        <div class="copy-right"> 
       <p style="letter-spacing:4px;border-radius:15px 0 15px 0;background-color:#000;padding-top:15px;padding-bottom:15px;width:100%">© <a href="index.html">P.N. COLLEGE </a>| DESIGNED BY <a href="http://facebook.com/incredible100rav"> INCREDIBLE SAURAV</a></p> 
      </div> 
</body> 
</html> 

任何人都可以幫助我,並指導我犯了什麼錯誤嗎?我知道代碼有點混亂,我很抱歉。我無法弄清楚我犯了什麼錯誤。

+0

旁註:'error_reporting(0);'和'@'符號不會幫助你的原因。您也過早關閉連接。 –

+0

在打開PHP標記 後立即在文件頂部添加錯誤報告,例如'<?php error_reporting(E_ALL); ini_set('display_errors',1);'然後你的代碼的其餘部分,看看它是否產生任何東西。 還要把'或者死(mysql_error())'添加到'mysql_query()'中。 –

+0

哦,我做錯了..在我的數據庫中沒有任何電子郵件或名稱....這是userName和userEmail ....我得到的結果corredtly ...但我只得到1值。我如何獲得所有內容? @ Fred-ii- –

回答

2

更改您的循環爲每一行取一個數組 -

while ($row = mysql_fetch_array($result)) { 

    $email = $row['email']; // assign this array part to a variable 
    $name = $row['name']; 

    echo $name ." " .$email . "<br />"; // echo the variables 

} 

// now you can close the connection, after you have used the results 
mysql_close(); 

stop using mysql_* functions。他們不再保持和officially deprecated。請改爲了解prepared statements,並考慮使用PDO,it's not as hard as you think

+0

哦,我做錯了..沒有任何電子郵件或名稱在我的數據庫....這是userName和userEmail ....我得到的結果corredtly ...但我越來越只有1個值。我如何獲得所有內容? @jay –

+0

我在這裏展示的循環應該會得到所有結果。 –

+1

@KumariAnjali或嘗試'mysql_fetch_assoc'而不是'mysql_fetch_array' –