如何回显 INNER 加入 PHP
How to echo INNER join in PHP
我已经在 MySQL 中测试了我的代码,它有效。我唯一的问题是我不明白如何回显 INNER JOIN(以前从未使用过),而且我似乎无法在网上找到明确的示例。
我需要将代码回显到 <table>
连接到数据库(有效):
include 'db_connection1.php';
$conn = OpenCon();
echo "Connected Successfully";
数据库连接到什么:
<?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->close();
}
?>
代码:
$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";
根据我的评论,如果您想在内容中显示查询,只需执行;
echo $sql;
(我知道这不是一个很好的答案 - 这是写下来的一种形式)
解释后编辑
根据您的评论,您想要 table 中的结果吗?
所以...
<?php
$table = "<table><tr><th>Order ID</th><th>First Name</th><th>Last Name</th><th>Order Date</th></tr>";
// Set up DB connection
$conn = new MySqli("db_hostname", "db_user", "db_pass", "db_name");
// Excecute the query
$result = $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");
// For each row, add the results to a table string using concatenate (.=)
while ($row = $result->fetch_assoc())
{
$table .= "<tr>";
$table .= "<td>{$row['OrderID']}</td>";
$table .= "<td>{$row['FirstName']}</td>";
$table .= "<td>{$row['LastName']}</td>";
$table .= "<td>{$row['OrderDate']}</td>";
$table .= "</tr>";
}
$table .= "</table";
print $table;
您的订单 table 似乎没有名为 Customer_Customer_ID
的列。所以我不知道你是如何声称查询有效的。
但是,添加一列 Customer_ID
,并将最后一行调整为:
INNER JOIN customer ON orders.Customer_ID=customer.Customer_ID";
I do not understand how I Echo an INNER JOIN
这里的重要概念是同一结果集中的 INNER JOIN(或与此相关的任何联接)returns。您可以获得很多行,但这不是通过使用连接来确定的。您可以简单地遍历结果集;每次迭代是 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>';
我已经在 MySQL 中测试了我的代码,它有效。我唯一的问题是我不明白如何回显 INNER JOIN(以前从未使用过),而且我似乎无法在网上找到明确的示例。
我需要将代码回显到 <table>
连接到数据库(有效):
include 'db_connection1.php';
$conn = OpenCon();
echo "Connected Successfully";
数据库连接到什么:
<?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->close();
}
?>
代码:
$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";
根据我的评论,如果您想在内容中显示查询,只需执行;
echo $sql;
(我知道这不是一个很好的答案 - 这是写下来的一种形式)
解释后编辑
根据您的评论,您想要 table 中的结果吗?
所以...
<?php
$table = "<table><tr><th>Order ID</th><th>First Name</th><th>Last Name</th><th>Order Date</th></tr>";
// Set up DB connection
$conn = new MySqli("db_hostname", "db_user", "db_pass", "db_name");
// Excecute the query
$result = $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");
// For each row, add the results to a table string using concatenate (.=)
while ($row = $result->fetch_assoc())
{
$table .= "<tr>";
$table .= "<td>{$row['OrderID']}</td>";
$table .= "<td>{$row['FirstName']}</td>";
$table .= "<td>{$row['LastName']}</td>";
$table .= "<td>{$row['OrderDate']}</td>";
$table .= "</tr>";
}
$table .= "</table";
print $table;
您的订单 table 似乎没有名为 Customer_Customer_ID
的列。所以我不知道你是如何声称查询有效的。
但是,添加一列 Customer_ID
,并将最后一行调整为:
INNER JOIN customer ON orders.Customer_ID=customer.Customer_ID";
I do not understand how I Echo an INNER JOIN
这里的重要概念是同一结果集中的 INNER JOIN(或与此相关的任何联接)returns。您可以获得很多行,但这不是通过使用连接来确定的。您可以简单地遍历结果集;每次迭代是 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>';