如何从其他db表中获得与数组值相关的值?

时间:2015-09-22 07:12:58

标签: php mysql

在下面的代码中,我将一个数组存储在customer表中。现在我想要代理表中的数据,但是谁与数组值相关。

 <?php
    $query = "SELECT * FROM customer";
    $no1=1;
    $result = mysql_query($query);

    while($values = mysql_fetch_array($result))
    {
        $values= unserialize($values['part_no']);


        foreach($values as $value)
        {

            echo $value."<br>";
            $query2 = "SELECT * FROM agent WHERE number=$value";
            $result2 = mysql_query($query2);

            while($values2 = mysql_fetch_array($result2))
            {

                echo '' . $row['name'] . '';   // get value from agent table.
                $no1++;
                // echo $no1."<br>";
            }
        }

    } 
?>

2 个答案:

答案 0 :(得分:0)

最好使用implode

while($values = mysql_fetch_array($result))
    {
        $val= unserialize($values['part_no']);


        $query2 = "SELECT * FROM agent WHERE number IN (".implode(',',$val).")";
            $result2 = mysql_query($query2);

            while($values2 = mysql_fetch_array($result2))
            {

                echo '' . $row['name'] . '';   // get value from agent table.
                $no1++;
                // echo $no1."<br>";
            }
    }

答案 1 :(得分:0)

试试这个

<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);

while($values = mysql_fetch_array($result))
{
    $values= unserialize($values['part_no']);


    foreach($values as $value)
    {
       $search_value = "'".implode("','", $value)."'";
        echo $value."<br>";
        $query2 = "SELECT * FROM agent WHERE number IN ($search_value)";
        $result2 = mysql_query($query2);

        while($values2 = mysql_fetch_array($result2))
        {

            echo '' . $values2['name'] . '';   // get value from agent table.
            $no1++;
            // echo $no1."<br>";
        }
    }

} 
?>