SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot AirFor's Avatar
    Join Date
    Oct 2010
    Location
    Berrigan, NSW, Australia
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help displaying database to HTML table

    So I'm trying to display a database in a HTML table using HTML and PHP to show the results. I'm a novice with PHP and trying to get the following to work:

    Code:
    <html>
    <head>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <script type="text/javascript" src="jquery-latest.js"></script>
    <script type="text/javascript" src="jquery.tablesorter.js"></script>
    </head>
    <body>
    
    <form action="insert.php" method="post">
    Game Name: <input type="text" name="gamename">
    Genre: <input type="text" name="genre">
    Metacritic Score: <input type="text" name="score">
    Completed?: <input type="text" name="completed">
    <input type="submit">
    <input type="reset">
    </form>
    
    <?php
    $username="dbelldes_dbell";
    $password="removed";
    $database="dbelldes_jos2";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM Games";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    ?>
    <table id="myTable" class="tablesorter">
    <thead>
    <tr>
    <th>Game Name</th>
    <th>Genre</th>
    <th>Metacritic Score</th>
    <th>Completed?</th>
    </tr>
    </thead>
    <tbody>
    
    <?php
    $i=0;
    while ($i < $num) {
    
    $f1=mysql_result($result,$i,"gamename");
    $f2=mysql_result($result,$i,"genre");
    $f3=mysql_result($result,$i,"score");
    $f4=mysql_result($result,$i,"completed");
    ?>
    
    <tr>
    <td><?php echo $f1; ?></td>
    <td><?php echo $f2; ?></td>
    <td><?php echo $f3; ?></td>
    <td><?php echo $f4; ?></td>
    </tr>
    
    <?php
    $i++;
    }
    ?>
    </tbody>
    </table>
    </body>
    </html>
    I'm not sure what the problem is?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Welcome to the world of Database Interaction.

    Please dont feel like i'm picking on you, but you've got quite a few things to adjust here.
    #0: mysql as a library is being deprecated; you should look at moving to mysqli or PDO.
    #1: mysql_numrows is not a function (mysql_num_rows)
    #2: Consider using a while construct that uses the result resource's own pointer (while($row = mysql_fetch_assoc($result)) { )
    #3: If you do #2, your $f definitions can be simplified using a list (list($f1,$f2,$f3,$f4) = $row)... or alternatively you can just use the $row variable in your echos (<?php echo $row['gamename']; ?>)
    #4: If you're doing this in a development area, you should consider turning error displays on (#1) until you get the script doing what it should be.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •