0

我正在尝试将我的数据从 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);
?>

输出如下 数据复制

我不想要任何重复的值谁能帮助我
真的很感激
谢谢

4

3 回答 3

0

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

SELECT DISTINCT FirstName,LastName FROM adel
于 2012-10-11T10:15:09.560 回答
0
SELECT * FROM adel GROUP BY P_ID
于 2012-10-11T10:24:51.823 回答
0

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

row1

row1 
row2

row1
row2
row3

row1
row2
row3
row4

row1
...

尝试:

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

顺便说一句 '$schema_insert .= "";' 什么都不做。

于 2012-10-11T11:49:40.763 回答