如何显示获取数组的结果表单?

时间:2016-03-22 08:48:39

标签: php html sql

数据库:

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

2 个答案:

答案 0 :(得分:1)

这是我能做到的答案。

$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));    

}

答案 1 :(得分:0)

您可以使用以下内容,

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

而不是

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