将mysql导出到csv时获取随机字段名称

时间:2013-07-23 14:06:04

标签: php mysql csv

我正在尝试将mysql表导出到csv。我遇到的问题是,每次代码递增时,它都会输入一个名为“id”的字段名称。所以在第一场之后,我得到了id。之后还有两个字段,我得到了id。之后的三个领域,我得到id字段。我不知道为什么会这样。任何帮助是极大的赞赏。 (注意:我知道不使用mysql,我通常使用mysqli,但这就是我现在使用的)。 这是我的代码:

    $hostname = "localhost"; //SET SERVER/HOSTNAME
    $dbusername = "username"; //SET DATABASE USERNAME
    $dbname = "dbname"; //SET DATABASE NAME
    $dbpassword = "password"; //SET DATABASE USERNAME

$dbhandle = mysql_connect($hostname, $dbusername, $dbpassword) 
  or die("Unable to connect to MySQL");

$selected = mysql_select_db($dbname,$dbhandle) 
  or die("Could not select Data Base");

    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");



$query = "SELECT * FROM v88374";

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

$fields = mysql_num_fields ( $export );

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

    echo $header;
}

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";                        
}

print "$header\n$data";

exit();

2 个答案:

答案 0 :(得分:1)

我会用

fputcsv()

以下文档中的示例

http://www.php.net/manual/en/function.fputcsv.php

答案 1 :(得分:0)

我认为对于下一次更改,这将是一种更好的方法,它更容易一些:

$query = "SELECT * FROM v88374";
$export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
$query = "SHOW COLUMNS FROM v88374";
$rfields = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
$rows = array();
$fields = array();

$tmp = mysql_fetch_assoc ( $rfields );
while($tmp)
{
    $fields[] = $tmp['Field'];
    $tmp = mysql_fetch_assoc ( $rfields );
}
@mysql_free_result($rfields);

$tmp = mysql_fetch_assoc ( $export );
while($tmp)
{
    $rows[] = $tmp;
    $tmp = mysql_fetch_assoc ( $export );
}
@mysql_free_result($export);

$header = implode("|\t", $fields);
$ifields = count($fields);
echo '<pre>';var_dump($ifields, $fields);
$data = '';
foreach($rows as $row)
{
    $line = '';
    for($i = 0; $i < $ifields; $i++)
        $line .= ($line == '' ? '' : "\t").$row[$fields[$i]];
    $data .= $line."\n";
}

echo $header."\n".$data;
exit();

或者

$query = "SELECT * FROM v88374";
$export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
$rows = array();
$fields = array();

$tmp = mysql_fetch_field ( $export );
while($tmp)
{
    $fields[] = $tmp->name;
    $tmp = mysql_fetch_field ( $export );
}

$tmp = mysql_fetch_assoc ( $export );
while($tmp)
{
    $rows[] = $tmp;
    $tmp = mysql_fetch_assoc ( $export );
}
@mysql_free_result($export);

$header = implode("|\t", $fields);
$ifields = count($fields);
echo '<pre>';var_dump($ifields, $fields);
$data = '';
foreach($rows as $row)
{
    $line = '';
    for($i = 0; $i < $ifields; $i++)
        $line .= ($line == '' ? '' : "\t").$row[$fields[$i]];
    $data .= $line."\n";
}

echo $header."\n".$data;
exit();