SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    print html table of mysql Database table

    Hello guys !!
    i would like to print all data of a database table to an HTML table
    i've tried different methods
    here's some of code :
    $sql = mysql_query("SELECT * FROM students ORDER BY date_added DESC");
    $itemsCount = mysql_num_rows($sql); // count the output amount

    if ($itemsCount > 0) {
    echo "<table border='1' id= results>
    <tr>
    <th>ID</th>
    <th>student name </th>
    <th>student age </th>
    <th>date added</th>
    </tr>";
    while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $student_name = $row["student_name"];
    $age = $row["student_age"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

    echo "<table border='1' id= results>";
    echo "<td>" .$id. "</td>";
    echo "<td>" .$student_name. "</td>";
    echo "<td>" .$age. "</td>";
    echo "<td>" .$date_added. "</td>";
    echo "</tr>";
    echo "</table>";

    }

    the table i got is not very organized (columns)
    any help plz !!!!!? i'm 12 yrs old newbie !!

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try replace with this code in your script:
    Code:
    if ($itemsCount > 0) {
    echo "<table border='1' id= results>
    <tr>
    <th>ID</th>
    <th>student name </th>
    <th>student age </th>
    <th>date added</th>
    </tr>";
    while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $student_name = $row["student_name"];
    $age = $row["student_age"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
      echo "<tr><td>" .$id. "</td>";
      echo "<td>" .$student_name. "</td>";
      echo "<td>" .$age. "</td>";
      echo "<td>" .$date_added. "</td></tr>";
    }
    echo "</table>";
    }
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners


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
  •