Log in

View Full Version : auto number column



Feckie
01-23-2009, 01:30 PM
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
$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);
?>

jackbenimble4
01-25-2009, 08:22 PM
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:



$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>";
}

Feckie
01-25-2009, 08:43 PM
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:



$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

Nile
01-25-2009, 08:46 PM
Shorter solution:


$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>";
}