在网页中查看数据库内容时需要一些帮助

时间:2015-05-11 13:53:09

标签: php sql

我试图将数据库的内容查看到网页中。我使用此代码:

<?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>

任何人都可以帮助我并指导我在哪里犯了错误吗?我知道代码有点乱,我很抱歉。我无法弄清楚我犯了哪个错误。

1 个答案:

答案 0 :(得分: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