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 ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=list-export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>