0

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

2 Answers 2

1

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';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi tried the solution you gave but still the same error
@Raj now it might be the solution
Thanks Oliver, i had done some mistake while trying it earlier did it again and it worked. Thanks again.
1

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.