duanfan8699 2014-11-13 03:09
浏览 36

使用不同的SQL查询多次将此MySQL重复为CSV

I have the following PHP which will return the results to my query to me in a CSV format, but the code (two letters) in the LIKE statement as shown at the top needs to change between multiple different codes.

I have about 30 different codes. I need to define all the codes such as:

CV, LC, RCA, JOR etc...

And have the script make a new CSV for each different code and quickly go and process each one, one after another. So I end up with 30 files for example. I may need to do this a few times so manually changing it 30 times is not my top option.

<?php

// database variables
$hostname = "localhost";
$user = "###";
$password = "###";
$database = "###";

$select = "select * from subscribers where list=27 and custom_fields LIKE '%\%CV\%%'";

$con = mysql_connect($hostname, $user, $password);
$db_selected = mysql_select_db($database, $con);

// Check connection
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "
";
}
$data = str_replace( "" , "" , $data );

if ( $data == "" )
{
    $data = "
(0) Records Found!
";
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=list-export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header
$data";

?>
  • 写回答

5条回答 默认 最新

  • dsw1608 2014-11-18 13:23
    关注

    The easiest way is to insert all your values in the query using OR operator:

    SELECT * 
    FROM subscribers
    WHERE list = 27 
    AND (
         custom_fields LIKE '%value_1%'
      OR custom_fields LIKE '%value_2%'
      OR custom_fields LIKE '%value_3%'
      OR custom_fields LIKE '%value_4%'
    )
    

    And so on.

    Please read why you shouldn't use mysql_* functons in PHP.

    Edit

    Sorry I did not understood your question this way.

    @RenéHoffmann's answer is the way to go, then.

    First create an array with all values and associated file:

    $data = array(
        'value1' => 'myvalue1.csv',
        'value2' => 'myvalue2.csv',
        'value3' => 'myvalue3.csv',
        ...
    ); 
    

    Then iterate this array:

    foreach ($data as $value => $file)
    {
        $escapedValue = mysql_real_escape_string($value);
        $select = "select * from subscribers where list=27 and field LIKE '%{$escapedData}%'";
        $response = mysql_query($select);
    
        $rows = array();
        while ($row = mysql_fetch_assoc($response))
        {
            $rows[] = $row;
        }
    
        $csv = array2csv($rows); // see https://stackoverflow.com/a/13474770/731138
        file_put_contents($file, $csv);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题