2017-09-08 18 views
0

儘管我已經定義了custname,日期和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 

回答

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'; 
} 
+0

嗨試過你給的解決方案,但仍然是相同的錯誤 – Raj

+0

@Raj現在它可能是解決方案 – Oliver

+0

感謝奧利弗,我在嘗試它之前做了一些錯誤,再次做了它,它的工作。再次感謝。 – Raj

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