sql-Abfrage in Datei speichern

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • sql-Abfrage in Datei speichern

    Hallo,

    ich bin blutiger Anfänger und habe in meinen Büchern und hier im Forum leider bislang nicht die richtigen Antworten gefunden.
    Mein Problem ist bestimmt zu einfach...

    ich habe eine Abfrage, die ich mit echo widergebe:

    [COLOR=#800000][COLOR=#800000]$liste[/COLOR][/COLOR]=[COLOR=#0000f0][COLOR=#0000f0]"SELECT * FROM tabelle1 WHERE Export='0000-00-00' AND Eingang <>'0000-00-00 00:00:00' "[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000];[/COLOR]
    [/COLOR]
    [COLOR=#800000][COLOR=#800000]$liste1[/COLOR][/COLOR]=mysql_query[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$liste[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]

    [COLOR=#ff0000][COLOR=#ff0000]while[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR]=mysql_fetch_assoc[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$liste1[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000])){[/COLOR][/COLOR]
    [COLOR=#ff0000][COLOR=#ff0000][COLOR=#0000f0][COLOR=#0000f0]echo "[/COLOR][/COLOR][COLOR=#000000]$row[/COLOR][COLOR=#0000f0][COLOR=#0000f0][ID], [/COLOR][/COLOR][COLOR=#000000]$row[/COLOR][COLOR=#0000f0][COLOR=#0000f0][Name], [/COLOR][/COLOR][COLOR=#000000]$row[/COLOR][COLOR=#0000f0][COLOR=#0000f0][KID], [/COLOR][/COLOR][COLOR=#000000]$row[/COLOR][COLOR=#0000f0][COLOR=#0000f0][KAnrede][COLOR=#0000f0][COLOR=#0000f0]<br>[/COLOR][/COLOR][COLOR=#008000][COLOR=#008000]\n[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]"[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000];[/COLOR][/COLOR][/COLOR][/COLOR]

    [COLOR=#0000f0][COLOR=#0000f0][COLOR=#ff0000][COLOR=#ff0000]....[/COLOR][/COLOR][/COLOR][/COLOR]

    [COLOR=black]Die Ausgabe mit "echo" klappt prima, ich erhalte alle Datensätze, die der Bedingung in der Abfrage entsprechen.[/COLOR]

    [COLOR=black]Jetzt möchte ich dieses Ergebnis in eine csv-Datei packen, um es anschließend per Mail zu versenden.[/COLOR]
    [COLOR=#000000]Das versuche ich mit folgendem:[/COLOR]

    [COLOR=#000000][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR]=fopen[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$datei[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]'w'[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR]
    [/COLOR]
    fputs[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]"[/COLOR][/COLOR]$row[COLOR=#0000f0][COLOR=#0000f0][ID];[/COLOR][/COLOR]$row[COLOR=#0000f0][COLOR=#0000f0][Name]"[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]
    [COLOR=black]fclose[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]
    [COLOR=#ff0000][COLOR=#ff0000]}...[/COLOR][/COLOR]

    [COLOR=#ff0000][COLOR=#ff0000][COLOR=black]Leider erhalte ich in meiner Datei nur den letzten Datensatz aus dem Array. Ich müßte also eine Schleife (wie oben vor der echo-Ausgabe) erstellen:[/COLOR][/COLOR][/COLOR]

    [COLOR=#ff0000][COLOR=#ff0000][COLOR=#000000]fputs[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR] (while ([COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR]=mysql_fetch_assoc[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$liste1[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000])[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]

    [COLOR=#ff0000][COLOR=#ff0000][COLOR=#000000][COLOR=black]bringt nur ne Fehlermeldung, die Datei bleibt leer.[/COLOR][/COLOR][/COLOR][/COLOR]


    [COLOR=#ff0000][COLOR=#ff0000][COLOR=#000000]Was mache ich falsch? Oder, wie bringe ich meine echo-Ausgabe in eine Textdatei?[/COLOR][/COLOR][/COLOR]

    [COLOR=#ff0000][COLOR=#ff0000][COLOR=#000000]Vielen Dank schon mal im voraus für Eure Zeit![/COLOR][/COLOR]
    [/COLOR]
    [/COLOR][/COLOR]
    [/COLOR]
    [/COLOR]
    [/COLOR]
    [/COLOR]

  • #2
    Warum verwendest du nicht fputcsv(), wenn du eine CSV-Datei erstellen wilst?

    PHP: fputcsv - Manual

    Kommentar


    • #3
      habe deinen Hinweis so eingebaut:

      [COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR]=fopen[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$datei[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]'w'[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);
      [/COLOR]
      [/COLOR]
      foreach[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000] [/COLOR][/COLOR]as[COLOR=#800000][COLOR=#800000]$fields[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000])[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]{
      [/COLOR]
      [/COLOR]
      fputcsv [COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fields[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);
      }
      [COLOR=black]fclose();[/COLOR]
      [COLOR=#000000][/COLOR]
      [COLOR=#000000]es kommt folgende Fehlermeldung:[/COLOR]
      Warning: fputcsv() expects parameter 2 to be array, string given in /dir/dir15738/bereich/csvexport1.php on line 54

      [COLOR=black]welche ich inhaltlich nicht verstehe. Parameter 2 sollte ein Array sein, ich habe aber einen String übergeben?[/COLOR]
      [COLOR=#000000]Aber $row ist doch mein Array?!? [COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000] [/COLOR][/COLOR]as[COLOR=#800000][COLOR=#800000]$fields[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000])[/COLOR][/COLOR][/COLOR]
      [COLOR=#000000][COLOR=#ff0000][COLOR=#ff0000][/COLOR][/COLOR][/COLOR]
      [COLOR=black]Mir fehlt gerade die Vorstellungskraft, einfach Datensätze (die über echo ausgegeben werden) in eine Datei zu schreiben.[/COLOR]
      [COLOR=black][/COLOR]
      [/COLOR]
      [/COLOR]

      Kommentar


      • #4
        Lass das foreach() weg. Wozu soll das gut sein?

        Kommentar


        • #5
          ... um mir alle Datensätze in die Datei zu schreiben.

          Ich habe es jetzt weggelassen und den 2. Parameter als $row angegeben:

          [COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR]=fopen[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$datei[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]'w'[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);
          [/COLOR]
          [/COLOR]
          fputcsv [COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]
          [COLOR=#ff0000][COLOR=#ff0000][/COLOR][/COLOR]
          [COLOR=black]Keine Fehlermeldung mehr, aber wieder nur den letzten Datensatz in der Datei. Laut Abfrage sind es aber 3.[/COLOR]

          Kommentar


          • #6
            Weil du vermutlich für jeden Datensatz eine neue Datei erstellst und die alte überschreibst.

            Kommentar


            • #7
              ja, klingt logisch.
              Wenn ich aber

              [COLOR=#800000]$fp[/COLOR]=fopen[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$datei[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]'w'[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);
              [/COLOR]
              [/COLOR]
              fputcsv [COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$row[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]

              aus meiner while-Schleife rausnehme....

              Und beim Schreiben dieser Zeilen hast Du mich auf den Weg gebracht:
              Ich mußte lediglich mein
              [COLOR=#800000][COLOR=#800000]$fp[/COLOR][/COLOR]=fopen[COLOR=#ff0000][COLOR=#ff0000]([/COLOR][/COLOR][COLOR=#800000][COLOR=#800000]$datei[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000],[/COLOR][/COLOR][COLOR=#0000f0][COLOR=#0000f0]'w'[/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000]);[/COLOR][/COLOR]
              [COLOR=black]aus der Schleife rausnehmen.[/COLOR]

              Ich danke vielmals!!! Ich geb' Dir ein Bier aus!

              Kommentar

              Lädt...
              X