Raj
Raj

Reputation: 1

Issue with undefined index Error

Though I have defined custname,Date and itemnum as array, while running the code i am getting error where ever i have used the above variables.

The error is given at the end

CODE

<?php
$con = mysqli_connect("localhost", "root", "","demo");
$custname=array();
$Date=array();
$itemnum=array();
$sql= "SELECT customermst.custid, customermst.custname,   purchasemst.purchasedt, purchasemst.itemnum FROM customermst, purchasemst WHERE purchasemst.custid = customermst.custid ORDER BY custid";
echo $sql;
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array( $result ,MYSQLI_BOTH))
{
    $custname[]=$row["customermst.custname"];
    $Date[]=$row["purchasemst.purchasedt"];
    $itemnum[]=$row["purchasemst.itemnum"];
}
echo '<html><head><title>Reports</title></head><body>
<table>
    <tr>
        <td>Customer Name</td><td>Date</td><td>Item Number</td>
    </tr>';
for($i=0; $i<=count($custname); $i++)
{
    echo '<tr><td>'.$custname[$i].'</td><td>'.$Date[$i].'</td>    <td>'.$itemnum[$i].'</td></tr>';
}
echo '</table></body></html>';
?>

ERROR

  ( ! ) Notice: Undefined index: customermst.custname in C:\wamp64\www\wordpress\project1\mastdtl.php on line 11
    Call Stack

#   Time    Memory  Function    Location
1   0.0013  238792  {main}( )   ...\mastdtl.php:0

( ! ) Notice: Undefined index: purchasemst.purchasedt in C:\wamp64\www\wordpress\project1\mastdtl.php on line 12
Call Stack
#   Time    Memory  Function    Location
1   0.0013  238792  {main}( )   ...\mastdtl.php:0

( ! ) Notice: Undefined index: purchasemst.itemnum in C:\wamp64\www\wordpress\project1\mastdtl.php on line 13
Call Stack
#   Time    Memory  Function    Location
1   0.0013  238792  {main}( )   ...\mastdtl.php:0

Upvotes: 0

Views: 200

Answers (2)

Adera
Adera

Reputation: 27

Do an inner join if the two tables are related as can be seen on your query, like so:

$sql= "SELECT DISTINCT customermst.custid, customermst.custname, purchasemst.purchasedt, purchasemst.itemnum FROM customermst INNER JOIN purchasemst ON customermst.custid = purchasemst.custid ORDER BY custid";

Upvotes: 1

Oliver
Oliver

Reputation: 151

You can just call the Attribute while fetching. No need to call the table with it:

$j = 0;
if($result->num_rows != 0)
{
     while($row=mysqli_fetch_array( $result ,MYSQLI_BOTH))
     {
          $custname[$j]=$row["custname"];
          $Date[$j]=$row["purchasedt"];
          $itemnum[$j]=$row["itemnum"];
          $j++;
      }
}
else
{
    print_r($result);
    echo 'nothing to fetch';
}

Upvotes: 1

Related Questions