如何回应INNER加入PHP

时间:2018-05-09 13:35:11

标签: php mysql echo

我已经在mySQL中测试了我的代码并且它有效,我唯一的问题是我不明白我是如何回应INNER JOIN的,(以前从未使用过它)我似乎无法在网上找到并清除示例,谢谢你对任何帮助的人

*需要将代码回显到<table> 连接到DB(Works)

*   include 'db_connection1.php';

    $conn = OpenCon();

    echo "Connected Successfully";

Db连接的内容:

<?php

function OpenCon()
 {
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "admin";
 $db = "theDBname";


 $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);


 return $conn;
 }

function CloseCon($conn)
 {
 $conn

- &GT;关();  }

&GT;

代码:

$sql = "SELECT orders.Order_ID AS OrderID,
            customer.First_Name AS FirstName, 
            customer.Last_Name AS LastName,
            orders.Order_Date AS OrderDate
            FROM Orders
            INNER JOIN customer ON orders.Customer_Customer_ID=customer.Customer_ID";

enter image description here

如果您还有其他需要,请询问

3 个答案:

答案 0 :(得分:1)

根据我的评论,如果你想在内容中显示查询,只需做;

$rating = 3;    
@for($x = 1; $x <= 5; $x++)
<img src="{{ URL::asset('assets/star.png')}}">
<!--<img src="{{ URL::asset('assets/ustar.png')}}"> --> //unfilled
@endfor

(我知道这不是一个很好的答案 - 这是写下来的形式)

解释后编辑

根据您的评论,您希望将结果放在表格中吗? 所以......

echo $sql;

答案 1 :(得分:0)

您的订单表似乎没有名为Customer_Customer_ID的列。因此,我不知道您如何宣称查询有效。

但是,添加一列Customer_ID,并将最后一行调整为:

INNER JOIN customer ON orders.Customer_ID=customer.Customer_ID";

答案 2 :(得分:0)

  

我不明白我是如何回应INNER JOIN

这里的重要概念是INNER JOIN(或任何连接)在同一结果集中返回。您可以获得许多行,但这不是通过使用连接来确定的。您可以简单地遍历结果集;每次迭代都是1行。

<?php
...
$results = $conn->query("SELECT orders.Order_ID AS OrderID,
    customer.First_Name AS FirstName, 
    customer.Last_Name AS LastName,
    orders.Order_Date AS OrderDate
    FROM Orders
    INNER JOIN customer ON orders.Customer_Customer_ID = 
    customer.Customer_ID");

print '<table border="1">';
while($row = $results->fetch_assoc()) {
    print '<tr>';
    print '<td>'.$row["OrderID"].'</td>';
    print '<td>'.$row["FirstName"].'</td>';
    print '<td>'.$row["LastName"].'</td>';
    print '<td>'.$row["OrderDate"].'</td>';
    print '</tr>';
}  
print '</table>';