如何显示获取数组的结果


How to show the result form getting array?

数据库:

  --> product table
    P_id    P_name          P_uploadKey
    1       Cemera          7365
    2       Notebook        7222
    3       Monitor         7355
    4       Printer         7242
    --> buy table
    B_id   P_id      B_name      date 
    1      1,3,4     somchai   12/3/2016
    2      2,3       kri       12/3/2016

此sql显示购买表上的查找id,其中$_GET['B_id'] = '2':

 $bid = $_GET['B_id'];
    $sqlB ="select * from buy where B_id ='$bid' ";
    $Recordset2 = mysql_query($sqlB, $connect) or die(mysql_error());
    $row_Recordset2 = mysql_fetch_assoc($Recordset2);

这个sql代码通过从上面的代码中获取$row_Recordset2['P_id'] like a 2,3来显示结果:

$pid = $row_Recordset2['P_id'];
$sqlp ="select * from buy where P_id ='$pid' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
}  while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));

我想这样显示,我们如何编辑它:

Notebook
Monitor

这是我能做到的答案。

$pid = $row_Recordset2['P_id'];
$array =  explode(',', $pid);
foreach ($array as $item) {

$sqlp ="select * from buy where P_id ='$item' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
}  while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));    
}

您可以像下面这样使用

$sqlp ="select * from product where P_id IN '($pid)'";

而不是

 $sqlp ="select * from buy where P_id ='$pid' ";