Guys,
Goodnight.
Today I come with another cool thing for you that I'm using in a project where I need to create an integration between two systems, one local and the other on the web: exporting data to a CSV file. This class is very similar to the one I showed in this post, with the difference is that now we are exporting data from a Firebird database (Interbase).
To do this, I created a class containing the function that we will use, available in this link.
Usage example:
<?php
require_once("classe.php");
// Conecta no banco de dados
$hostname = "localhost:C:\Dados\Banco_Firebird.FDB";
$usuario = "SYSDBA"; // Usuário padrão do Firebird
$senha = "masterkey"; // Senha padrão do Firebird
$conexao = ibase_connect( $hostname, $usuario, $senha ) or die( 'Erro ao conectar: ' . ibase_errmsg() );
// Exporta os dados
$classe = new Exporta();
$classe->exportFirebirdToCsv("SELECT * FROM cores", "cores.csv");
?>
And this will generate a file with the following:
ID,STATUS,MARCA,DATA_INC,DATA_ALT,DATA_HAB,NOME,DATA_COM,COD_INTELIGENTE,ID_COLECAO,DATA_PUBLIC
"1","N","N","2007-08-14","2007-08-14",,"UNICA","2007-08-14 17:32:59",,,
"2","N","N","2007-08-14","2007-08-14",,"BRANCA","2007-08-14 17:33:35",,,
"3","N","N","2007-08-14","2007-08-14",,"AMARELA","2007-08-14 17:33:47",,,
"4","N","N","2007-08-14","2007-08-17",,"AZUL CLARO","2007-08-17 15:24:53",,,
"5","N","N","2007-08-14","2007-08-14",,"VERDE","2007-08-14 17:34:19",,,
"12","N","N","2007-08-17","2007-08-17",,"DIVERSAS","2007-08-17 17:10:28",,,
"6","N","N","2007-08-14","2007-08-14",,"ROSA","2007-08-14 17:34:29",,,
"7","N","N","2007-08-14","2007-08-14",,"LARANJA","2007-08-14 17:34:40",,,
"8","N","N","2007-08-14","2007-08-14",,"LILAS","2007-08-14 17:34:53",,,
"9","N","N","2007-08-14","2007-08-14",,"PRETO","2007-08-14 17:35:20",,,
"10","N","N","2007-08-17","2007-08-17",,"AZUL ESCURO","2007-08-17 15:25:09",,,
"11","N","N","2007-08-17","2007-08-17",,"AZUL MARINHO","2007-08-17 15:25:37",,,
"13","N","N","2007-08-17","2007-08-17",,"PINK","2007-08-17 17:44:10",,,
"14","N","N","2007-08-17","2007-08-17",,"VERMELHA","2007-08-17 18:13:37",,,
And that's it, folks! Until next time!
For those who don't want to download the class, follow its code below:
<?php
class Exporta
{
public function exportFirebirdToCsv( $strQuery, $Ds_Arquivo_Destino = 'export.csv' )
{
$Ds_Separador_Linha = "\n";
$Ds_Separador_Coluna = ";";
$Ds_Caracter_Encapsular = '"';
$Ds_Caracter_Escape = "\\";
// Recupera os dados do servidor
$Ds_Retorno = ibase_query( $strQuery );
$Nr_Colunas = ibase_num_fields( $Ds_Retorno );
$Ds_Linha_CSV = '';
for ( $i = 0; $i < $Nr_Colunas; $i++ )
{
$arrColuna = ibase_field_info( $Ds_Retorno, $i );
$Ds_Linha_CSV .= $arrColuna['name'] . $Ds_Separador_Coluna;
}
$Ds_Saida = trim( substr( $Ds_Linha_CSV, 0, -1 ) );
$Ds_Saida .= $Ds_Separador_Linha;
while ( $Ds_Linha_Banco = ibase_fetch_row( $Ds_Retorno ) )
{
$Ds_Linha_CSV = '';
for ( $j = 0; $j < $Nr_Colunas; $j++ )
{
if ( $Ds_Linha_Banco[$j] == '0' || $Ds_Linha_Banco[$j] != '' )
{
if ( $Ds_Caracter_Encapsular == '' )
{
$Ds_Linha_CSV .= $Ds_Linha_Banco[$j];
}
else
{
$Ds_Linha_CSV .= $Ds_Caracter_Encapsular . str_replace( $Ds_Caracter_Encapsular, $Ds_Caracter_Escape . $Ds_Caracter_Encapsular, $Ds_Linha_Banco[$j] ) . $Ds_Caracter_Encapsular;
}
}
else
{
$Ds_Linha_CSV .= '';
}
if ( $j < $Nr_Colunas - 1 )
{
$Ds_Linha_CSV .= $Ds_Separador_Coluna;
}
} // end for
$Ds_Saida .= $Ds_Linha_CSV;
$Ds_Saida .= $Ds_Separador_Linha;
} // end while
// Grava o arquivo físico
if ( !is_dir( dirname( $Ds_Arquivo_Destino ) ) )
{
mkdir( dirname( $Ds_Arquivo_Destino ), 0755, true );
}
$criarArquivo = (!is_file( $Ds_Arquivo_Destino ) );
$objTxt = fopen( $Ds_Arquivo_Destino, "w" );
if ( $criarArquivo)
{
//UTF-8
fwrite( $objTxt, pack( "CCC", 0xef, 0xbb, 0xbf ) );
}
fwrite( $objTxt, $Ds_Saida );
fclose( $objTxt );
}
}
?>
Comentários (0)
Carregando comentários…