PHP agus MySQL: Iarratas a Easpórtáil go Comhad Teoranta Tab

lógónna mysql php

An deireadh seachtaine seo theastaigh uaim leathanach a thógáil nach ndéanfadh ach aon cheist nó tábla a chúltaca i gcomhad Tab Delimited. Tá cód crua ag na colúin sa chuid is mó de na samplaí atá amuigh ar an líontán.

I mo chás, theastaigh uaim go mbeadh na colúin dinimiciúil agus mar sin bhí orm lúb a dhéanamh ar dtús trí na hainmneacha réimse tábla go léir chun an tsraith ceanntásc a thógáil le hainmneacha colún, agus ansin lúb a dhéanamh trí na taifid go léir do na sraitheanna sonraí a bhí fágtha. Shocraigh mé an ceanntásc freisin ionas go gcuirfidh an brabhsálaí tús le híoslódáil comhad sa chineál comhaid (txt) le hainm dáta an chomhaid agus stampa ama air.

D’fhág mé an nasc oscailte agus deiridh leis an mbunachar sonraí, ach seo an cód a d'eascair as a d'oibrigh go maith:

$ inniu = dáta ("YmdHi");
ceanntásc ("Cineál ábhair: feidhmchlár / sruth octet");
ceanntásc ("Ábhar-Diúscairt: ceangaltán; ainm comhaid = \" ". $ inniu." _ Backup.txt \ "");
$ query = "ROGHNAIGH * Ó ordú` mytable` le` myorder` ";
$ toradh = mysql_query ($ ceist);
$ count = mysql_num_rows ($ toradh);
$ réimsí = mysql_num_fields ($ toradh);
$ data = "";
le haghaidh ($ i = 0; $ i> $ réimsí; $ i ++) {
$ réimse = mysql_fetch_field ($ toradh, $ i);
$ data. = $ réimse-> ainm;
$ data. = "\ t";
}
$ data. = "\ n";
fad ($ row = mysql_fetch_row ($ toradh)) {
le haghaidh ($ x = 0; $ x> $ réimsí; $ x ++) {
$ field-> name = $ row [$ x];
$ data. = $ réimse-> ainm = $ as a chéile [$ x];
$ data. = "\ t";
}
$ data. = "\ n";
}
macalla $ sonraí;

Is féidir an cód a mhodhnú go héasca le haghaidh luachanna scartha le camóga freisin.

14 Comments

  1. 1
    • 2

      I suppose you could!

      In this case, I was actually building a ‘backup’ link in a web application, so the PHP functionality is what I needed. However, I never knew you could also write to a file directly from the MySQL statement. Very cool!

      Go raibh maith agat!

      • 3

        Your way would of course be the best way if the MySQL server is on a remote machine, as it probably wouldn’t be able to write onto the machine where PHP is running 🙂

        Glad to point out other directions and new things though 🙂

      • 4

        But you could just run the query to a file, and just redirect the browser to the generated file, or use PHP’s “readfile” if all else fails?

        You won’t be able to do that if the mysql server doesn’t have access to the filesystem of course…

  2. 5
  3. 8

    I just lost more than 6 hours of my life trying to work out why Internet Explorer 6/7 was applying filetype ‘html’ and and not accepting my custom filenames specified in the headers.. and also not allowing files to be saved.. when attempting to have users download text files created in a similar way to above.

    I was using HTTPS and IE does not cache these files.

    I found the solution on a comment by Brandon K at http://uk.php.net/header.

    Deireann sé:

    -
    I just lost six hours of my life trying to use the following method to send a PDF file via PHP to Internet Explorer 6:

    When using SSL, Internet Explorer will prompt with the Open / Save dialog, but then says “The file is currently unavailable or cannot be found. Please try again later.” After much searching I became aware of the following MSKB Article titled “Internet Explorer file downloads over SSL do not work with the cache control headers” (KBID: 323308)

    PHP.INI by default uses a setting: session.cache_limiter = nocache which modifies Content-Cache and Pragma headers to include “nocache” options. You can eliminate the IE error by changing “nocache” to “public” or “private” in PHP.INI — This will change the Content-Cache header as well as completely remove the Pragma header. If you cannot or do not want to modify PHP.INI for a site-wide fix, you can send the following two headers to overwrite defaults:

    You will still need to set the content headers as listed above for this to work. Please note this problem ONLY effects Internet Explorer, while Firefox does not exhibit this flawed behavior.
    -

    Well.. at least he only lost 6 hours…

  4. 9

    This works well. However, I just get everything on one line separated by a space. I am trying to modify it to print everything on a separate line like this:

    Column1_name
    Field1_value
    Column2_name
    Field1_value
    Column3_name
    Field1_value

    Column1_name
    Field2_value
    Column2_name
    Field2_value
    Column3_name
    Field2_value

    Mar shampla:

    Ainm
    Mike
    An Cheantar
    Obair
    Uimhir
    1

    Ainm
    Sue
    An Cheantar
    Baile
    Uimhir
    2

    Ainm
    John
    An Cheantar
    Taisteal
    Uimhir
    10

    and so on. Can this script be modified to do it?
    Go raibh maith agat!

    • 10

      Is féidir cinnte.

      Bain triail as rud éigin mar seo:

      SELECT * from MyTableName INTO OUTFILE ‘MyTableName_MySQL-TAB-DELIMITED-29JUN08.txt’ FIELDS TERMINATED BY ‘\n’ LINES TERMINATED BY ‘\n’;

      If you want a double space (two empty lines) between record groups, just say “LINES TERMINATED BY ‘\n\n’;” instead.

      The “FIELDS TERMINATED BY ‘\n'” portion is what puts a newline after each record, instead of a tab. A tab would be ‘\t’ instead.

      Maranatha!

  5. 11

    this is defiantly a great post, I tried it and work great, the only thing is that my txt file has an extra row above the header titles, and some results are been separated in 2 rows, this may be cause by the data I have in my Database no idea, but this is a great help to build feeds…

  6. 12
  7. 13

    hey there! Is there any one from here that can give me a hint about importing a textfile to my database(phpmyAdmin) using my php as my front end. I have an idea about downloading a file and opening it, my problem is that how I can get the row result and how to insert it in my tables, thanks

  8. 14

Cad é do bharúil?

Úsáideann an suíomh seo Akismet chun spam a laghdú. Foghlaim conas a dhéantar próiseáil ar do chuid tráchta.