需要帮助。当列customer_id ='0'的列值时,我需要用值'Guest'替换它。这是我的代码,我已经尝试了一切我没能成功的事情。提前谢谢。
<?php
//Connection to database
mysql_connect("localhost","Username","Password");
mysql_select_db("DB_Name");
$result = mysql_query("SELECT
`order`. order_id,
`order`. customer_id,
`order`. payment_company,
`order`. payment_address_1,
`order`. payment_address_2,
`order`. payment_city,
`order`. payment_zone,
`order`. payment_postcode,
`order`. payment_firstname,
`order`. payment_lastname,
`order`. telephone,
`order`. email
FROM `order`
INNER JOIN customer ON `order`.customer_id = customer.customer_id
WHERE order_status_id > 0 ORDER BY order_id");
if (!$result) die('Sorry, I was unable to any fetch records. Try again');
$num_fields = mysql_num_fields($result);
$headers = array();
// Creating headers for output files
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
// name of file with date
$filename = "Filename-".date('Y-m-d').".csv";
// Setting header types for csv file.
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
}
?>
答案 0 :(得分:0)
怎么样?
while ($row = mysql_fetch_row($result))
{
$row[1] = (($row[1] == '0') ? 'Guest' : 0);
fputcsv($fp, array_values($row));
}
答案 1 :(得分:0)
SELECT order.order_id,
CASE WHEN order.customer_id = '0' THEN 'Guest' ELSE order.customer_id END AS customer_id,
...
答案 2 :(得分:0)
你必须在fputcsv之前进行攻击
while ($row = mysql_fetch_row($result))
{
if($row[1] == '0') {
$row[1] = 'Guest';
}
fputcsv($fp, array_values($row));
}
同时查看while($ row = mysql_fetch_assoc($ result)),你可以使用$ row ['customer_id']代替$ row [1]
答案 3 :(得分:0)
$result = mysql_query("SELECT
`order`.order_id,
IF (`order`.customer_id = 0, 'Guest', `order`.customer_id) AS customer_id,
`order`.payment_company,
etc..");