gpt4 book ai didi

php - 如何在 PHP 中回显 INNER join

转载 作者:行者123 更新时间:2023-11-29 10:14:57 24 4
gpt4 key购买 nike

我已经在 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";

Enter image description here

最佳答案

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

echo $sql;

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

解释后进行编辑

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

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

关于php - 如何在 PHP 中回显 INNER join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com