How to write value stored in a variable to CSV in MySQLHow to output MySQL query results in CSV format?How do I quickly rename a MySQL database (change schema name)?Can I concatenate multiple MySQL rows into one field?Which MySQL data type to use for storing boolean valuesParameterize an SQL IN clauseHow to output MySQL query results in CSV format?Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?How to declare a variable in MySQL?Writing a pandas DataFrame to CSV fileHow do I import an SQL file using the command line in MySQL?

Is there a way to make an animal companion able to read a language?

How is this situation not a checkmate?

The answer is a girl's name (my future granddaughter) - can anyone help?

Isn't the detector always measuring, and thus always collapsing the state?

Job interview by video at home and privacy concerns

Can I bring this power bank on board the aircraft?

Decision Variable Value from a Set (Gurobi)

Parent asking for money after I moved out

Everyone Gets a Window Seat

Could the Queen overturn the UK Supreme Court ruling regarding prorogation of Parliament?

How to identify whether a publisher is genuine or not?

Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?

Why the first octet of a MAC address always end with a binary 0?

How can I find places to store/land a private airplane?

PhD Length: are shorter PhD degrees (from different countries) valued differently in other counter countries where PhD Is a longer process?

Avoiding dust scattering when you drill

How to "Start as close to the end as possible", and why to do so?

Would a horse be sufficient buffer to prevent injury when falling from a great height?

Disable all sound permanently

Does Bank Manager's discretion still exist in Mortgage Lending

Booting Ubuntu from USB drive on MSI motherboard -- EVERYTHING fails

Why not add cuspidal curves in the moduli space of stable curves?

Why has Speaker Pelosi been so hesitant to impeach President Trump?

What are one's options when facing religious discrimination at the airport?



How to write value stored in a variable to CSV in MySQL


How to output MySQL query results in CSV format?How do I quickly rename a MySQL database (change schema name)?Can I concatenate multiple MySQL rows into one field?Which MySQL data type to use for storing boolean valuesParameterize an SQL IN clauseHow to output MySQL query results in CSV format?Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?How to declare a variable in MySQL?Writing a pandas DataFrame to CSV fileHow do I import an SQL file using the command line in MySQL?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















MySQL: I want to write a timestamp stored in a variable to a CSV file and I only know how to store a query.










share|improve this question
























  • Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

    – JNevill
    Mar 28 at 21:05











  • Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

    – Diogo Crava
    Mar 28 at 21:08






  • 1





    You can totally do a SELECT without a FROM. Check out an example here.

    – JNevill
    Mar 28 at 21:12











  • Thank you! Thats exactly the example I needed. I couldnt find any.

    – Diogo Crava
    Mar 28 at 21:27











  • "Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

    – Raymond Nijland
    Mar 28 at 21:36


















0















MySQL: I want to write a timestamp stored in a variable to a CSV file and I only know how to store a query.










share|improve this question
























  • Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

    – JNevill
    Mar 28 at 21:05











  • Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

    – Diogo Crava
    Mar 28 at 21:08






  • 1





    You can totally do a SELECT without a FROM. Check out an example here.

    – JNevill
    Mar 28 at 21:12











  • Thank you! Thats exactly the example I needed. I couldnt find any.

    – Diogo Crava
    Mar 28 at 21:27











  • "Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

    – Raymond Nijland
    Mar 28 at 21:36














0












0








0








MySQL: I want to write a timestamp stored in a variable to a CSV file and I only know how to store a query.










share|improve this question














MySQL: I want to write a timestamp stored in a variable to a CSV file and I only know how to store a query.







mysql sql csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 21:02









Diogo CravaDiogo Crava

63 bronze badges




63 bronze badges















  • Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

    – JNevill
    Mar 28 at 21:05











  • Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

    – Diogo Crava
    Mar 28 at 21:08






  • 1





    You can totally do a SELECT without a FROM. Check out an example here.

    – JNevill
    Mar 28 at 21:12











  • Thank you! Thats exactly the example I needed. I couldnt find any.

    – Diogo Crava
    Mar 28 at 21:27











  • "Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

    – Raymond Nijland
    Mar 28 at 21:36


















  • Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

    – JNevill
    Mar 28 at 21:05











  • Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

    – Diogo Crava
    Mar 28 at 21:08






  • 1





    You can totally do a SELECT without a FROM. Check out an example here.

    – JNevill
    Mar 28 at 21:12











  • Thank you! Thats exactly the example I needed. I couldnt find any.

    – Diogo Crava
    Mar 28 at 21:27











  • "Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

    – Raymond Nijland
    Mar 28 at 21:36

















Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

– JNevill
Mar 28 at 21:05





Can you put the variable in a SELECT statement and then write that to the csv the way you know how to?

– JNevill
Mar 28 at 21:05













Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

– Diogo Crava
Mar 28 at 21:08





Im kinda new to MySQL so Im not sure how to do that. Would that be something like "Select VARIABLE Into File 'hello.csv' "? Not sure if I can ignore the FROM keyword.

– Diogo Crava
Mar 28 at 21:08




1




1





You can totally do a SELECT without a FROM. Check out an example here.

– JNevill
Mar 28 at 21:12





You can totally do a SELECT without a FROM. Check out an example here.

– JNevill
Mar 28 at 21:12













Thank you! Thats exactly the example I needed. I couldnt find any.

– Diogo Crava
Mar 28 at 21:27





Thank you! Thats exactly the example I needed. I couldnt find any.

– Diogo Crava
Mar 28 at 21:27













"Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

– Raymond Nijland
Mar 28 at 21:36






"Not sure if I can ignore the FROM keyword." Well it's indeed not allowed by SQL standards, so tableless are not portable.. The SQL standard defines using SELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2) instead off SELECT 1 AS column1, 'one' AS column2 for example.. Only problem not all databases support the VALUES() for tableless selects also some database implement it with a dummy table like Oracle database DUAL table, which are works in MySQL and MariaDB.

– Raymond Nijland
Mar 28 at 21:36













1 Answer
1






active

oldest

votes


















0
















Here's how I do it (using my own SQL() function to retrieve the result set via fetch_all(MYSQLI_ASSOC), but you're clearly ok on that bit):



$out = fopen($file, 'w');
$rows = SQL($sqlstatement);
fputcsv($out, array_keys($rows[0]));
foreach($rows as $row) fputcsv($out, $row);
fclose($out);


The key is the built-in PHP fputcsv function. The first use of fputcsv writes the column headings (what a fantastic language PHP is...), the others (in the loop) write the rows.






share|improve this answer




























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406806%2fhow-to-write-value-stored-in-a-variable-to-csv-in-mysql%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    Here's how I do it (using my own SQL() function to retrieve the result set via fetch_all(MYSQLI_ASSOC), but you're clearly ok on that bit):



    $out = fopen($file, 'w');
    $rows = SQL($sqlstatement);
    fputcsv($out, array_keys($rows[0]));
    foreach($rows as $row) fputcsv($out, $row);
    fclose($out);


    The key is the built-in PHP fputcsv function. The first use of fputcsv writes the column headings (what a fantastic language PHP is...), the others (in the loop) write the rows.






    share|improve this answer































      0
















      Here's how I do it (using my own SQL() function to retrieve the result set via fetch_all(MYSQLI_ASSOC), but you're clearly ok on that bit):



      $out = fopen($file, 'w');
      $rows = SQL($sqlstatement);
      fputcsv($out, array_keys($rows[0]));
      foreach($rows as $row) fputcsv($out, $row);
      fclose($out);


      The key is the built-in PHP fputcsv function. The first use of fputcsv writes the column headings (what a fantastic language PHP is...), the others (in the loop) write the rows.






      share|improve this answer





























        0














        0










        0









        Here's how I do it (using my own SQL() function to retrieve the result set via fetch_all(MYSQLI_ASSOC), but you're clearly ok on that bit):



        $out = fopen($file, 'w');
        $rows = SQL($sqlstatement);
        fputcsv($out, array_keys($rows[0]));
        foreach($rows as $row) fputcsv($out, $row);
        fclose($out);


        The key is the built-in PHP fputcsv function. The first use of fputcsv writes the column headings (what a fantastic language PHP is...), the others (in the loop) write the rows.






        share|improve this answer















        Here's how I do it (using my own SQL() function to retrieve the result set via fetch_all(MYSQLI_ASSOC), but you're clearly ok on that bit):



        $out = fopen($file, 'w');
        $rows = SQL($sqlstatement);
        fputcsv($out, array_keys($rows[0]));
        foreach($rows as $row) fputcsv($out, $row);
        fclose($out);


        The key is the built-in PHP fputcsv function. The first use of fputcsv writes the column headings (what a fantastic language PHP is...), the others (in the loop) write the rows.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 28 at 23:11

























        answered Mar 28 at 22:55









        MandyShawMandyShaw

        9903 gold badges11 silver badges21 bronze badges




        9903 gold badges11 silver badges21 bronze badges

































            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406806%2fhow-to-write-value-stored-in-a-variable-to-csv-in-mysql%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현