未定义索引错误的问题

时间:2017-09-08 09:29:06

标签: php

虽然我已将custname,Date和itemnum定义为数组,但在运行代码时,我在使用上述变量时遇到错误。

错误在最后给出

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 个答案:

答案 0 :(得分:1)

您可以在获取时调用属性。无需用它调用表格:

$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';
}

答案 1 :(得分:1)

如果两个表相关,则进行内连接,如查询中所示,如下所示:

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