-1

Undefined index for the last element of array while fetching data from a database into an array variable. I use a for loop to seperate and also in_array()... kindly help pls...

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>";
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Apr 06 '13 at 10:32

2 Answers2

0

Add

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

Inside the foreach to prevent errors

powtac
  • 40,542
  • 28
  • 115
  • 170
0

Your code is horrible.

Use foreach

<font> is deprecated. $feild_name is spelled incorrectly.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is sloppy.

Erik
  • 222
  • 1
  • 5