Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    1

    Unanswered: PLZ HELP ME with printing SQL DB using PHP

    Greetings everyone!
    I dont know if you guys can solve it.. maybe u'll :O

    Well I have a db in mysql called lst1 and a table called paddlers. In order to print all the columns of the table in a php page, my code is

    PHP Code:
    <?
    mysql_connect
    ("localhost""root""");
    mysql_select_db("lst1");
    $result mysql_query("SELECT * FROM paddlers");
    ?>
    <table border="1" cellpadding="0" cellspacing="0">
      <tr>
        <th>ID</th>
        <th>Paddle No</th>
        <th>L/R</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
        <th>Team</th>
        <th># Home</th>
        <th># Cell</th>
        <th>Email</th>
        <th>Login</th>
        <th>Password</th>
        <th>Practice Attendance</th>
      </tr>

    <?
    while ($row mysql_fetch_array($result)) {
        echo 
    "<tr>";
        echo 
    "<td>" $row['id'] . "</td>";
        echo 
    "<td>" $row['no'] . "</td>";
        echo 
    "<td>" $row['side'] . "</td>";
        echo 
    "<td>" $row['firstname'] . "</td>";
        echo 
    "<td>" $row['lastname'] . "</td>";
        echo 
    "<td>" $row['age'] . "</td>";
        echo 
    "<td>" $row['team'] . "</td>";
        echo 
    "<td>" $row['phonehome'] . "</td>";
        echo 
    "<td>" $row['phonecell'] . "</td>";
        echo 
    "<td>" $row['email'] . "</td>";
        echo 
    "<td>" $row['login'] . "</td>";
        echo 
    "<td>" $row['password'] . "</td>";
        echo 
    "<td>" $row['practiceattendance'] . "</td>";
        echo 
    "</tr>";
    }
    mysql_free_result($result);
    mysql_close();
    ?>

    </table>
    Is is working great, but now the problem is that each time i have a column I got to add manually like retype the html code and the echo column name. I have another php page that when it $post something with a form, it automaticly creates a column in the paddlers table. So to print it, I have to manually add it (<th> xxxx</th> and echo "<td>" . $row['xxx'] . "</td>" .

    I am a newbie so please when replying, write the code according to the name of my table

    I would like to know if it is possible to have a code (and in which php page do i paste the code) so it can automaticly prints the newly column added without i haveing to do it manually.

    If it is not possible, is there a way to print all the columns and the rows of a database in a table in php without having to manually edit a php page each time a column of the db is created or deleted?

    Thanks in advance guys!

  2. #2
    Join Date
    Mar 2007
    Location
    010101010110100
    Posts
    803
    Hello. To do what you are asking, you will need to use an array. Have a look at this:
    http://www.w3schools.com/php/php_arrays.asp
    If you are starting out in php, I think you will understand this a little better than php's site.

    If I have a chance, I will post back with an example for you.

  3. #3
    Join Date
    Mar 2007
    Location
    010101010110100
    Posts
    803
    This is what you need. It should get you started anyhow. This will enable you to add columns in your paddlers table and then display them without having to hardcode the columns into your php script.
    PHP Code:
      echo "<table><tr><th>Header</th></tr><tr><td><table>";
      foreach(
    $result as $key)
      {
        echo 
    "<tr>";
        foreach(
    $key as $attr => $value)
        {
          echo 
    "<th>".$attr."</th><td>".$value."</td>";
        }
        echo 
    "</tr>";
      }
      echo 
    "</table></td></tr></table>"
    Quote Originally Posted by l4cky
    I have another php page that when it $post something with a form, it automaticly creates a column in the paddlers table.
    That does not sound like a good design IMHO. You may want to rethink this a bit.

Posting Permissions

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