PHP / MySQL关系/回声场

时间:2015-11-09 10:43:33

标签: php mysql

我正在尝试构建我的第一个关系数据库,到目前为止看起来像这样:

关系表

Relationship Table

合作伙伴表

Partners

位置表

Locations

我希望回显所有行信息,包括我页面上的相关位置名称,如何让以下内容回显?

ID: 2 名称: Salisbury删除地点:Salisbury
ID: 4 名称:中间删除位置:Salisbury,Southampton
ID: 5 名称:南安普顿搬迁地点:南安普敦

=====解决!=====

$sql = "SELECT partner_id, partner_name, email_address, active FROM partners WHERE active ='yes' ORDER BY partner_id ASC";


                                $connect->query($sql);

                                if ($partners = $connect->query($sql)) {

                                    foreach ($partners as $partner) {

                                        echo '<li><ul>';
                                        echo '<li>' . $partner['partner_id'] . '</li>';
                                        echo '<li>' . $partner['partner_name'] . '</li>';
                                        echo '<li>' . $partner['email_address'] . '</li>';
                                        echo '<li>' . $partner['active'] . '</li>';

                                        // START GET LOCATIONS FROM RELATED TABLE
                                        echo '<ul>';

                                        $sql2 = "SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from partners_locations r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id =" . $partner['partner_id'] . "";

                                        $connect->query($sql2);

                                        if ($locations = $connect->query($sql2)) {

                                            foreach ($locations as $location) {

                                                echo '<li>' . $location['Locations'] . '</li>';
                                            }

                                        } else {

                                            echo "Error: No Locations<br>";

                                        }

                                        echo '</ul>';
                                        // END GET LOCATIONS FROM RELATED TABLE

                                        echo '</ul></li>';

                                    }

                                } else {

                                     echo "Error: No Active Partners<br>";

                                }

4 个答案:

答案 0 :(得分:2)

您可以做的最好的事情是制作一个新表格,以包含partner_idlocation_id

<强> tbl_relationships_new

enter image description here

此方法的优点: -

1)。当您需要从合作伙伴中删除位置时,您无需编辑列locations。您只需删除此新表中的条目即可。

2)。当您需要在locations字段中添加更多数据时,您只需插入新表即可,这比更新partners.locations更容易。

现在,您可以使用简单left joins来获取所需的数据。 我的表解决方案的SQL查询。

SELECT t.*,p.*,l.* FROM tbl_relationships_new t, partners p, locations l LEFT JOIN 
    partners 
    ON
    t.partner_id = p.partner_id 
    LEFT JOIN 
    locations 
    ON 
    l.location_id = t.location_id 
    WHERE
    t.partner_id = 2

<强>更新

以下是基于您的表格结构的查询。

1)。 ID:2名称:Salisbury搬迁地点:Salisbury

SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 2

enter image description here

2)。当有2个地点时。

SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 5

enter image description here

<强>更新

没有明确提及ID的解决方案。

SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id

答案 1 :(得分:0)

<?php
    $dbconn = mysqli_connect(DBSERVER, DBUSER, DBPWD, DBNAME);
    $sql = "SELECT partner_id, partner_name, locations FROM Partners";
    $result = mysqli_query($dbconn, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        echo "ID: ".$row['partner_id']." Name: ".$row['partner_name']." Locations: ";
        $locs = explode(",",$row['locations']);
        $first = true;
        foreach ($locs as $loc) {
            $sql2 = "SELECT location_name FROM Locations WHERE location_id = '".$loc."'";
            $result2 = mysqli_query($dbconn, $sql2);
            $row2 = mysqli_fetch_assoc($result2);
            $separator = ($first?"":", ");
            $first = false;
            echo $separator.$row2['location_name'];
        }
        echo "<br/>";
    }
?>

答案 2 :(得分:-1)

首先,您的表表:合作伙伴条目应该是这样的。

partener_id   | partner_name     |  email_address | active | location_id
2               Test Partner        .......         yes         1
5               Good Removals       .......         yes         1
5               Good Removals       .......         yes         2
4               Special Removals    .......         yes         2

因此,您可以使用表格:地点进行加入。

之后执行以下sql查询。

 $sql = "select * from partners Left join locations on locations.location_id=partners.location_id";

为了更好地理解查询执行和数据显示。请点击以下链接。

http://www.w3schools.com/php/php_mysql_select.asp

答案 3 :(得分:-1)

您的&#34;地点&#34;合作伙伴表中的字段是错误的,您必须创建另一个表来处理合作伙伴和位置之间的关系,称为Locations_Partners par example。此表必须包含2个字段,location_id和partner_id。这样,您就可以将许多合作伙伴与多个地点联系起来。然后,您的代码应如下所示:

<?php
$servername = "www.server.com";
$username = "username";
$password = "password";
$dbname = "your database name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT partner_id, partner_name, location_name FROM Partners 
INNER JOIN Locations_Partners ON Partners.partner_id = Locations_Partners.Partner_id
INNER JOIN Locations ON Locations_Partners.Location_id = Locations.location_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["partner_name"]. " " . $row["location_name"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>