Php 如何使用fputcsv拆分sql查询的导出
我有一个用于sql查询的简单fputcsv命令。但有时我的导出有更多的行和不同的主键。我的愿望是使用不同的主键将每个结果拆分为新的csv导出 例如:Php 如何使用fputcsv拆分sql查询的导出,php,csv,row,multiple-columns,fputcsv,Php,Csv,Row,Multiple Columns,Fputcsv,我有一个用于sql查询的简单fputcsv命令。但有时我的导出有更多的行和不同的主键。我的愿望是使用不同的主键将每个结果拆分为新的csv导出 例如: | *row 1 | row 2 | row 3 | |.......27 |......45 |......aa | |.......27 |......35 |......ab | |.......28 |......85 |......bb | |.......28 |......65 |......bc | 实际情况:第1行有
| *row 1 | row 2 | row 3 |
|.......27 |......45 |......aa |
|.......27 |......35 |......ab |
|.......28 |......85 |......bb |
|.......28 |......65 |......bc |
实际情况:第1行有主键(order_id)。我每天都要处理所有的订单。因此,该文件有多个订单(如上面的示例=订单27、订单28等)。我的供应商需要为每个订单提供一个csv文件。所以我需要一个代码,谁将查询拆分为“如果订单id不同,那么创建一个新文件…等等…”。这可能吗
<?php
$servername = "Jarvis";
$username = "TonyStark";
$password = "iLoveIronMan";
$dbname = "TheAnvengers";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT o.order_id, o.customer_id, op.quantity, op.model FROM oc_order o INNER JOIN oc_order_product op ON o.order_id = op.order_id INNER JOIN oc_product p ON op.product_id = p.product_id WHERE o.order_status_id = 2 AND p.location = 1 ORDER BY o.order_id, op.model";
$file = fopen('../files/in/filename.csv', 'w');
if ($rows = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($rows)) {
fputcsv($file, $row, ';');
}
mysqli_free_result($rows);
}
mysqli_close($conn);
fclose($file);
?>
为什么不使用order\u id作为文件名,将fopen
和fclose
移动到循环中,并将fopen
更改为在追加模式下工作 您的代码足够清晰,但请更明确地说明您要实现的目标,例如示例SQL结果和希望从中获取的CSV文件。谢谢。在上述原始帖子中完成;-)谢谢你的建议。我已经有了一个带有这个愿望的帖子。但这并不能解决我的问题。供应商需要为每个订单提供一个单独的csv,数据库中有许多订单在同一个列表中。你在开玩笑吗?这是我两个问题的解决方案!非常感谢你。它起作用了。仅用于理解您的答案:按顺序分割结果的部分在哪里?
<?php
// connect to the database
$servername = "Jarvis";
$username = "TonyStark";
$password = "iLoveIronMan";
$dbname = "TheAnvengers";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the data
$sql = "SELECT o.order_id, o.customer_id, op.quantity, op.model FROM oc_order o INNER JOIN oc_order_product op ON o.order_id = op.order_id INNER JOIN oc_product p ON op.product_id = p.product_id WHERE o.order_status_id = 2 AND p.location = 1 ORDER BY o.order_id, op.model";
$orders = array(); // For storing the data by order_id
// Loop through the result set and populate $orders
if ($rows = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($rows)) {
// This is where the split on order_id occurs
$orders[$row['order_id']][] = $row;
/*
$orders will now look like this after the first iteration
$orders = array(
27 => array(
0 => array(
'order_id' => 27,
'customer_id' => 45,
'quantity' => aa,
'model' => someModel
)
)
);
==============================
$orders will look like this after the last iteration
$orders = array(
27 => array(
0 => array(
'order_id' => 27,
'customer_id' => 45,
'quantity' => aa,
'model' => someModel
),
1 => array(
'order_id' => 27,
'customer_id' => 35,
'quantity' => ab,
'model' => someModel
),
28 => array(
0 => array(
'order_id' => 28,
'customer_id' => 85,
'quantity' => bb,
'model' => someModel
),
1 => array(
'order_id' => 28,
'customer_id' => 65,
'quantity' => bc,
'model' => someModel
)
);
*/
}
mysqli_free_result($rows);
}
mysqli_close($conn);
// Loop through $orders (by order_id)
foreach($orders as $id => $order)
{
// Open the file for writing, blanking existing file or creating if non-existent - name it after the order_id
$f = fopen('../files/in/' . $id . '.csv', 'w');
// Loop through each $row for that particular order_id and put it into the csv
foreach($order as $entry)
{
fputcsv($f, $entry, ';');
}
fclose($f);
}
?>