0

How do I get all data from one row from mysql table exported into text files, but formatted this way:

  • one field under another, one per line
  • i would like to break that data into pieces and save for example 50 lines in file1.txt then next 50 in file2.txt and so on until end (it's not round number so last file would have less lines probably)
  • don't copy identical entries / remove duplicates

...using php script or just mysql console?

2 Answers 2

2

I might do it this way, using the command-line client:

$ mysql --user=XXX --password=XXX --batch --skip-column-names \
  -e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Very cool. Reminids of those great perl one liners. Oh yeah, you forgot the distinct ;)
Heh! See my favorite Perl hack here: stackoverflow.com/questions/303876/…
0

I hate doing peoples homework/work, but this one is just too easy.

    <?

    $row = mysql_fetch_array($rc);

    $nodupes = array();
    foreach ($row as $field)
    {
        $nodupes[$field] = $field;
    }

    $i = 1;
    $filenum = 0;
    $line = 50 ; // create new file ever 50 lines
            $text = "";
    foreach($nodupes as $field)
    {
        $i++;
        $text .= "$field\n";

        // to split into bite size peices
        if ($i % $line == 0)
        {
            $filenum++;
            $filename = "myfile_$filenum.txt";
            file_put_contents($filename,$text);
            $text = "";
        }
    }
    $filenum++;
    $filename = "myfile_$filenum.txt";
    file_put_contents($filename,$text);
    ?>

So eat today, and learn to fish another day!

4 Comments

So, he's going to copy & paste it and learned nothing :) But you damn right *g
You can't copy and paste forever. Eventually he will either learn, or hire someone like you or I to clean up his mess.
I'd have used a 'SELECT DISTINCT' rather than manually remove the dupes, or at the very least, use PHP's built in array_unique.
it's neither work nor homework ;), i'm trying to learn on sample database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.