【发布时间】:2019-02-03 01:26:23
【问题描述】:
我一直在寻找解决此问题的方法,但我发现的一切对于我正在尝试做的事情来说似乎都过于复杂。
我正在尝试获取属于客户组 #2 的所有客户的列表。据我所知,这部分工作正常。
接下来,我正在尝试查找他们的订单并输出他们公司的名称。这里我认为我的代码有错误导致挂断。
因此,最终目标是列出属于第 2 组的客户的所有公司名称。
我正在尝试通过 SQL 查询来做到这一点。网页只是在加载时挂起。有一次我让它显示一些数据,但它不完整。
以下是我所拥有的。你能看一下,看看有什么问题吗?我希望有一个不合适的地方;或类似的东西。
* 出于安全原因,我没有粘贴连接详细信息,但代码确实可以毫无问题地连接到数据库。
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Run a Query to get all wholesale customer IDs
$sql1 = "SELECT * FROM `customer_entity` WHERE `group_id` = 2";
$customerIDs = $conn->query($sql1);
//so now that we have the wholesale customer IDs we are going to search
their orders for their company names.
while( $row1 = $customerIDs->fetch_assoc() )
{
//Run a query to get a list of orders that the customer has placed
$sql2 = "SELECT * FROM `sales_flat_order_address` WHERE
`customer_id` = " . $row1["entity_id"] ."";
$customerOrders = $conn->query($sql2);
// Echo the orders company name
while( $row2 = $customerOrders->fetch_assoc() )
{
//Check to see if the name is NULL
if($row2['company'] !== NULL)
{
//display the company name.
echo $row2['company'] . "</br>";
}
}
}
//close the connection
mysqli_close($conn);
?>
【问题讨论】: