Insert into CSV file

Asked

Viewed 75 times

0

I wanted to insert the table data (Sqlite) into a CSV file. I do a query first, and I want to insert the result of that query into the CSV file. Exists in PHP the function fputcsv. I can create the file but wanted to insert. Here’s the code:

for ($i=0; $i<=$nombre;$i++){
        $requete    =   "SELECT * FROM contact WHERE $selectoption='$search' AND id=$i";    
        $resultat   =   $base_hndl->query($requete);    
        $affiche    =   $resultat->fetchArray();//

        echo "<tr>";
            echo "<td>$affiche[nom]</td>";
            echo "<td>$affiche[prenom]</td>";
            echo "<td>$affiche[fonction]</td>";
            echo "<td>$affiche[societe]</td>";
            echo "<td>$affiche[mobile]</td>";
            echo "<td>$affiche[mail]</td>";
        echo "</tr>";

    }

    $fp = fopen($search.".csv", 'w');

        fputcsv($fp, $list);
    fclose($fp);
    echo "ficheiro csv criado";
}
  • http://meta.pt.stackoverflow.com/questions/297/quando-se-deve-colocar-o-nome-da-linguagem-no-t%C3%ADtulo/1911#1911

1 answer

1

Save your data to the $list?

$list = array();
for ($i=0; $i<=$nombre;$i++){
        ...
        $list[] = $affiche;
        ...
    }

    $fp = fopen($search.".csv", 'w');

        fputcsv($fp, $list);
    fclose($fp);
    echo "ficheiro csv criado";
}
  • right but shows nothing in the file.

  • Make a print_r($list) to see what data is there before writing to the file.

  • Yes, from the fputcsv line error. Array to string Conversion

  • But what is the content of your $list? Print to see what’s there or it’s hard to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.