从mysql转移到ms-excel

时间:2012-10-11 10:11:42

标签: php mysql excel

我正在尝试将我的数据从mysql传输到ms-excel,但我正在使用php获取重复值

<?php
        $form='';
    $link = odbc_connect ('Ayush', '', '');
if (!$link)
{
    die('Could not connect: ' . odbc_error());
}
    echo 'Connected successfully .<br>';
    //create query to select as data from your table
    $sql= "SELECT * FROM adel";
    $result =  odbc_exec($link,$sql);
    $sep = "\t"; //tabbed character 
    $fp = fopen('database.xls', "w"); 
    $schema_insert = ""; 
    $schema_insert_rows = ""; 
    for ($i = 1; $i < (odbc_num_fields($result))+1; $i++) 
    { 
    $schema_insert_rows.=odbc_field_name($result,$i) . "\t"; 
    } 
    $schema_insert_rows.="\n"; 
    echo $schema_insert_rows; 
    fwrite($fp, $schema_insert_rows); 
    //start while loop to get data 
    while($row = odbc_fetch_array($result)) 
    { 
        foreach($row as $value)
        {
        //set_time_limit(60); // 
        /*$schema_insert = ""; 
        for($j=1; $j<odbc_num_fields($result);$j++) 
        { 
        if(!isset($row[$j])) 
        $schema_insert .= "NULL".$sep;
        elseif  ($row[$j] != "") */
        $schema_insert.= strip_tags("$value").$sep; 

        $schema_insert .= "";
        } 

    //$schema_insert = str_replace($sep."$", "", $schema_insert); 

    //this corrects output in excel when table fields contain \n or \r 
    //these two characters are now replaced with a space 

    //$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
    $schema_insert .= "\n"; 
    //$schema_insert = (trim($schema_insert)); 
    //print $schema_insert .= "\n"; 
    print "\n";
    fwrite($fp, $schema_insert); 
    } 
    fclose($fp);
odbc_close($link);
?>

,输出如下 Data Duplication

我不想要任何贬义的价值观 任何人都可以帮助我 真的很感激它 Thankx

3 个答案:

答案 0 :(得分:0)

您可以在查询中添加DISTINCT或GROUP BY。例如:

SELECT DISTINCT FirstName,LastName FROM adel

答案 1 :(得分:0)

SELECT * FROM adel GROUP BY P_ID

答案 2 :(得分:0)

你没有重新初始化$ schema_insert在循环顶部获取记录,因此你的输出将是:

row1

row1 
row2

row1
row2
row3

row1
row2
row3
row4

row1
...

尝试:

while($row = odbc_fetch_array($result)) 
{ 
   $schema_insert='';
   ...

用'$ schema_insert。=“”;'来排除这一行什么也没做。