Results 1 to 4 of 4

Thread: auto number column

  1. #1
    Join Date
    Dec 2005
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default auto number column

    I am using the following to get details from my database,

    Is it possible to add an auto number column at the beginning of each line




    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","user","pass");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("database name"$con);

    $result mysql_query("SELECT * FROM database name");

    echo 
    "<center>
    <h1><b>Members</b></h1>
    <table border='1'>
    <tr>
    <th>Created</th>
    <th>Username</th>
    <th>Name</th>
    <th>Passphrase</th>
    <th>Custom1</th>
    </tr></center>" 
    ;

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['Created'] . "</td>";
      echo 
    "<td>" $row['Username'] . "</td>";
      echo 
    "<td>" $row['Name'] . "</td>";
      echo 
    "<td>" $row['Passphrase'] . "</td>";
      echo 
    "<td>" $row['Custom1'] . "</td>";
      echo 
    "</tr>";
      }
    echo 
    "</table>";
    mysql_close($con);
    ?>

  2. #2
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    I'm not sure what you mean. MySQL allows for auto-incrementing columns which are useful for referencing specific rows. Every single one of my MySQL tables have an auto-incrementing primary key, with my sessions table being the only exception.

    If you just want to count off the results you get from the database, you could just do this:

    PHP Code:
    $i 0;
    while(
    $row mysql_fetch_array($result))
      {
      
    $i++;
      echo 
    "<tr>";
      echo 
    "<td>"$i "</td>";
      echo 
    "<td>" $row['Created'] . "</td>";
      echo 
    "<td>" $row['Username'] . "</td>";
      echo 
    "<td>" $row['Name'] . "</td>";
      echo 
    "<td>" $row['Passphrase'] . "</td>";
      echo 
    "<td>" $row['Custom1'] . "</td>";
      echo 
    "</tr>";
      } 

  3. The Following User Says Thank You to jackbenimble4 For This Useful Post:

    Feckie (01-25-2009)

  4. #3
    Join Date
    Dec 2005
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jackbenimble4 View Post
    I'm not sure what you mean. MySQL allows for auto-incrementing columns which are useful for referencing specific rows. Every single one of my MySQL tables have an auto-incrementing primary key, with my sessions table being the only exception.

    If you just want to count off the results you get from the database, you could just do this:

    PHP Code:
    $i 0;
    while(
    $row mysql_fetch_array($result))
      {
      
    $i++;
      echo 
    "<tr>";
      echo 
    "<td>"$i "</td>";
      echo 
    "<td>" $row['Created'] . "</td>";
      echo 
    "<td>" $row['Username'] . "</td>";
      echo 
    "<td>" $row['Name'] . "</td>";
      echo 
    "<td>" $row['Passphrase'] . "</td>";
      echo 
    "<td>" $row['Custom1'] . "</td>";
      echo 
    "</tr>";
      } 
    Thats exactly what I wanted, many thanks

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Shorter solution:
    Code:
    $i = 0; 
    while($row = mysql_fetch_array($result)) 
      { 
      echo "<tr>"; 
      echo "<td>". ++$i . "</td>"; 
      echo "<td>" . $row['Created'] . "</td>"; 
      echo "<td>" . $row['Username'] . "</td>"; 
      echo "<td>" . $row['Name'] . "</td>"; 
      echo "<td>" . $row['Passphrase'] . "</td>"; 
      echo "<td>" . $row['Custom1'] . "</td>"; 
      echo "</tr>"; 
      }
    Jeremy | jfein.net

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
  •