Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将自动编号列添加到从mysql读取的php表中_Php_Sql_Auto Increment - Fatal编程技术网

将自动编号列添加到从mysql读取的php表中

将自动编号列添加到从mysql读取的php表中,php,sql,auto-increment,Php,Sql,Auto Increment,我试图在第一列之前添加一列,该列从1开始对每条记录进行编号。我正试图将自动增量添加到该行回音中$第['ss'行];,但这似乎并不奏效。有什么办法吗 我的代码如下所示: $result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC"); echo "<table border='0'> <tr> <th al

我试图在第一列之前添加一列,该列从1开始对每条记录进行编号。我正试图将自动增量添加到该行回音中$第['ss'行];,但这似乎并不奏效。有什么办法吗

我的代码如下所示:

$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");

echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

在标题中添加此行

<th align='left'>#</th>
这里是php代码

$count = 1;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
//other code
$count=$count+1;
}

更好的解决方案是使用变量计数器对行进行编号:

$counter = 1;

while($row = mysqli_fetch_array($result))
   echo "<td>" . $counter . "</td>";
   $counter++;
}

这是正确的方法,因为如果ss列是mysql自动增量,然后,您的行将不会按照排序进行编号,而是按照它们插入数据库的顺序进行编号,而不考虑您应用的任何排序。

尝试使用fetch\u assoc更改fetch\u数组。

如果您认为这有助于您感谢,请将其标记为应答。我正在获取每条记录的数字,但右侧列中的数据不显示。我不能100%确定在哪里输入您提供的代码。你能告诉我吗?