如何在PHP应用程序中打印mysql中的行号
作者:互联网
我有一个mysql表需要显示数据以及前端应用程序中的行号.以下查询在phpMyadmin中完美运行
SET @row_num=0; SELECT (@row_num:=@row_num+1) AS num,INV,DOS,PTNAME,BAL,PROV from sheet;
但是当我在php项目中使用相同的代码时,它返回以下错误
警告:mysql_fetch_array():提供的参数不是有效的MySQL
以下是我的代码:
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("omega",$conn);
$sel="SET @row_num=0; SELECT (@row_num:=@row_num+1) AS num,INV,DOS,PTNAME,BAL,PROV from sheet";
$sqlquery=mysql_query($sel);
while($dis=mysql_fetch_array($sqlquery))
{
echo"<tr>";
echo "<td>".$dis['num']."</td>";
echo "<td>".$dis['INV']."</td>";
echo "<td>".$dis['DOS']."</td>";
echo "<td>".$dis['PTNAME']."</td>";
echo "<td>".$dis['BAL']."</td>";
echo "<td>".$dis['PROV']."</td>";
echo"</tr>";
}
解决方法:
改变
$sel="SET @row_num=0; SELECT (@row_num:=@row_num+1) AS num,INV,DOS,PTNAME,BAL,PROV from sheet";
至
$sel="SELECT s.*, @rownum := @rownum + 1 AS num FROM sheet s, (SELECT @rownum := 0) r";
标签:row-number,php,mysql 来源: https://codeday.me/bug/20190828/1754515.html