比较两个结果并显示结果PHP

时间:2019-09-16 21:20:13

标签: php mysql

我正在尝试获取两个相同的ID,并将其转换为名称,这更好地解释了我有一个ID-COMPANY NAME表,并且在产品表ID COMPANY中-产品名,我想将两个和下一个匹配我将在excel文件中输出。 我测试了这段代码,但是所有结果都对我不显示:

while(($row = mysqli_fetch_array($result)) && ($row2 = mysqli_fetch_array($result2)) )
  {

答案:

db:

1)id-名称公司-ecc //表公司
2)id_product-产品-id_company //表产品

输出excel:

客户端1-ID 客户端2-ID

名称|其他信息
客户1 | eccc ...
客户2 | …

Php:

$result = mysqli_query($connect, $query);   // FIRST CONNECT TABLE
$result2 = mysqli_query($connect, $query2); // SECOND CONNECT TABLE
if(mysqli_num_rows($result2) && mysqli_num_rows($result)> 0)
{
$output2 .= '
<table class="table" id="table" bordered="1">

';
while($row2 = mysqli_fetch_array($result2))
{


$output2 .= '
<tr>
<td class="grassetto">Company:</td>
<td class="grassetto">'. $row2["id"].'-'. $row2["nomeazienda"].'</td>
</tr>
<tr>
<td class="grassetto">ID:</td>
<td class="grassetto">'. $mese.'/'.$anno.'</td>
</tr>
';
}
$output2 .= '</table>';

$output .= '
<table class="table ops" id="table" border="1">
<tr>
<th>Name Company</th>
<th>Another information</th>

</tr>
';
while($row = mysqli_fetch_array($result))
{

$output .= '
<tr>
<td align="left">'. $row['id_azienda'] . '</td>
<td align="left">'. $row['nr'] . '</td>



</tr>
';
}
$output .= '</table>';

header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
header("Pragma: no-cache");
header("Expires: 0");
echo $output2. "\n<br>" .$output."\n";


}

所以在第二个输出中,当我调用$ result时,我需要比较ID / ID_COMPANY并写上公司名称

编辑2: 那是主要查询

$query = 'SELECT * FROM  products WHERE  id_company in ('.$company.') AND product ="'.$products.'"';
        }else{
            $query = 'SELECT * FROM  products WHERE  id_company IN ('.$company.') AND product ="'.$products.'" AND last_check LIKE "%'.$anno.'-'.$mese.'%"';
            }

        $query2 = 'SELECT * FROM  company WHERE  id in ('.$company.')';

您如何集成此代码?

1 个答案:

答案 0 :(得分:1)

使用简单的JOIN通过一次查询即可从产品表中的公司ID中获取公司名称。

$query = "
    SELECT c.name, p.product, ...
    FROM company AS c
    JOIN product AS p ON c.id = p.id_company
    WHERE c.id in ($company) AND p.product = '$product'";