因此,我要尝试从三个表中获取数据。
客户表 订单表 和零件表
我试图基本上生成一个报告,以显示用户购买了什么以及花费了多少。这涉及从客户那里获取他们的accountID,将其与在Orders中附加到OrderID的accountID一起加入,然后在订单中获取PartID,附加到orderID,并返回partName。
我当时想的是遍历一个php数组,并使用sql用customerNames填充它,然后对其他查询使用sql来按名称对所有内容进行排序。尽管不确定INNER JOIN在尝试查询3个表时对我不起作用,但不确定这是否是最佳方法。这是我的数组代码:
$sql = "SELECT firstName FROM Customers INNER JOIN Orders ON
Customers.accountID=Orders.accountID";
$statement = $conn->query($sql);
$i = 0;
$NameArray = array();
while ($Names = $statement->fetch()) {
echo "<br>";
echo $Names[0];
$NameArray[$i];
echo "<br>";
$i++;
}
这给出了错误的未定义索引。不太确定为什么
答案 0 :(得分:0)
$sql = "SELECT firstName FROM Customers INNER JOIN Orders ON
Customers.accountID=Orders.accountID";
$statement = $conn->query($sql);
$i = 0;
$NameArray = array(); ***CREATES an empty array
while ($Names = $statement->fetch()) {
echo "<br>";
echo $Names[0];
$NameArray[$i]; ***Should start with 'echo'.
Also, there's nothing assigned to $NameArray[$i]
echo "<br>";
$i++;
}
在上面的代码中,我发现了2个问题并将其记录下来,但是都不能解决索引错误。您仍必须为$ NameArray [$ i]分配一个值,然后才能在不出现索引错误的情况下回显它。
关于循环查找信息,您可以像这样将[Cost]字段添加到SQL中:
SELECT Customers.firstName, Orders.Cost, Orders.PartID FROM Customers INNER JOIN Orders ON
Customers.accountID=Orders.accountID
您可以随后通过以下联接将“零件”表添加到查询中:
Orders.PartID = Parts.PartID
然后只需将Parts.partName添加到SELECT字段中。
答案 1 :(得分:0)
我猜你正在运行mysqli
您将需要列出客户表和订单表的所有表列。
现在要进行测试,正在为客户添加列名称,为订单表添加数量。
让我们假设我们要从数据库中选择“ Customers.name”和“ Orders.quantity”
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "your-db"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$query = 'SELECT Customers.accountID, Customers.name, Orders.quantity, Orders.accountID FROM Customers
LEFT JOIN Orders ON Customers.accountID=Orders.accountID
ORDER BY Customers.accountID';
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)){
$order_quantity = $row['quantity'];
$customer_name = $row['name'];
//you can now echo
echo $customer_name.' '.$order_quantity.'<br/>';
}
?>
现在,由于您要生成数组,因此代码如下所示
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "your-db"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$res_arr = array();
$query = 'SELECT Customers.accountID, Customers.name, Orders.quantity, Orders.accountID FROM Customers
LEFT JOIN Orders ON Customers.accountID=Orders.accountID
ORDER BY Customers.accountID';
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)){
$order_quantity = $row['quantity'];
$customer_name = $row['name'];
$res_arr[] = array("customer name" =>$customer_name, "order quqntity" =>$order_quantity);
}
echo json_encode($res_arr);
exit
?>
或者如果您不希望对结果数组响应进行参数初始化,则可以按照以下说明进行操作。
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$res_arr[] = $row;
}
echo json_encode($res_arr);
exit;
答案 2 :(得分:-1)
实际上$ NameArray是一个空数组。
可能您想要以下内容: $ NameArray [$ i] = $ Names [0]; 并且在循环结束时,您将在$ NameArray中拥有所有的名字
答案 3 :(得分:-1)
按名称排序可以使mysql更好
$sql = "select firstName
from Customers AS Customers
inner join Orders AS Orders on Customers.accountID = Orders.accountID
GROUP BY Customer.accountID
ORDER BY Customer.firstName";
...
$i=0;
while($Names=$statement->fetch()){
echo $Names[$i];
$i++;
}