数组的最后一个元素出现未定义的索引错误


Undefined index error for the last element of array

从数据库向数组变量中获取数据时,数组最后一个元素的未定义索引。我使用for循环来分离,也使用in_array()。。。请帮忙。。。

include("includes/global_inc.php");
$qry1=$_REQUEST['qry'];
$feilds2=$_REQUEST['fields1'];
$cnt=$_REQUEST['count'];
$feild_name=explode(",",$feilds2);
$iquery=mysql_query("$qry1") or die(mysql_error());
echo "<table border='1'>";
echo "<tr>";
for($i=0;$i<$cnt;$i++) {
echo "<th>".$feild_name[$i]."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>";
}
echo "</tr>";
for($g=0;$g<$cnt;$g++) {
    while($test=mysql_fetch_array($iquery)) {
        echo "<tr align='center'>";
              echo"<td><font color='black'>".$test[$feild_name[$g]]."</font></td>";
          echo"</tr>";
      }
  }
echo "</table>";

添加

If (isset($feild_name[$i])) {

在foreach内部防止错误

您的代码非常糟糕。

使用foreach

<font>已弃用。$feild_name拼写错误。

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;太草率了。