php
2016-11-24 44 views -1 likes 
-1

我想创建一个表,我可以在一个变量(将发送给js稍后),然后显示它。但我的回声结果只是表头('Produkty','Ilość','Cena')。你能告诉我我犯了什么错误或纠正了我吗?需要有人来纠正我的mysql查询表创建

<?php 

header('Access-Control-Allow-Origin: *'); 
include "database.php"; 


$dane = array(); 
$tabela='<table class="table table-striped"> 
    <thead> 
     <tr> 
     <th>Produkt</th> 
     <th>Ilość</th> 
     <th>Cena</th> 
     </tr> 
    </thead> 
    <tbody>'; 
$dane=array(); 

$sql_main="SELECT Products.`Name`,Orders_NEW.`Amount`,((Products.`Price`)*(Orders_NEW.`Amount`)) as 'PRICE' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669"; 

$dane = $db->query($sql_main); 

foreach($dane as $row) 
{ 
    $tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>"; 
} 

$sql_second="SELECT SUM((Products.`Price`)*(Orders_NEW.`Amount`)) as 'SUMA' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669"; 

$dane_second= array(); 

$dane_second= $db -> query($sql_second); 

foreach($dane_second as $row) 
{ 

$tabela.='<thead> 
     <tr> 
     <th>Łącznie</th> 
      <th></th> 

     <th>'.$row["SUMA"].'</th> 
     </tr> 
    </thead> 
</table>'; 
} 

echo($tabela); 



?> 

编辑:的foreach改为而($行= $ dane-> FETCH_ASSOC()) 现在我的结果是:

PRODUKTIlość以上的价格

Łącznie

好像变量像$ row ['Name']等是这里的问题

+1

http://php.net/manual/en/function.error-reporting.php --- http://php.net/manual/en/mysqli.error.php - 调试是50%的乐趣。 –

+0

请确保存在一个订单与编号669,你有这个'Order_ID = 669'在你的SQL代码 – BrunoM24

+0

是啊我有Order_ID与编号为669,试图SQL我的SQL服务器上查询 – TheWebWeeb

回答

0

我不确定你是否有某种自定义类与database.php,但如果$ db只是合作ntains mysqli的对象,那么你需要做到以下几点:

$dane = $db->query($sql_main); 

while($row = $dane->fetch_assoc()) { 
    $tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>"; 
} 

同去的第二个查询(假设你的SQL是正确的,顺便说一下返回结果)。

+0

它做了一些事情,但仍然没有工作,更新了我的帖子 – TheWebWeeb

+0

更新:刚刚重新打印querrys和一切正常完美。你的Loop帮了我很多。谢谢 – TheWebWeeb

0

你必须在代码中的错误:

foreach($dane_second as $row){ 
    $tabela.='<thead> 
    <tr> 
     <th>Łącznie</th> 
     <th></th> 
     <th>'.$row["SUMA"].'</th> 
    </tr> 
    </thead> 
    </table>'; //Here's the problem 
} 
echo($tabela); 

移动收表外的foreach。

相关问题