PHP:SQL值作为文件名 [英] PHP: SQL value as filename
本文介绍了PHP:SQL值作为文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
<?php
$servername = "Jarvis";
$username = "TonyStark";
$password = "iLoveIronMan";
$dbname = "StarkCompany";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
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);
?>
现在:如何获取表o.order_id的SQL值并将其用作文件名?文件名应为example(o.order_id).csv.这可能吗?
Now: How can i take for example the SQL value of the table o.order_id and use this value for my filename? The filename should be example(o.order_id).csv. Is this possible?
推荐答案
通过命名如下值可以使您的生活更轻松:
You can make your life easier by naming the value like this:
$sql = "SELECT o.order_id as filename, 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";
然后在处理行时可以使用
then when you handle the rows you can use
$rows['filename']
这篇关于PHP:SQL值作为文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文