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;
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
|
show 5 more comments
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
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 aSELECT
without aFROM
. 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 usingSELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2)
instead offSELECT 1 AS column1, 'one' AS column2
for example.. Only problem not all databases support theVALUES()
for tableless selects also some database implement it with a dummy table like Oracle databaseDUAL
table, which are works in MySQL and MariaDB.
– Raymond Nijland
Mar 28 at 21:36
|
show 5 more comments
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
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
mysql sql csv
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 aSELECT
without aFROM
. 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 usingSELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2)
instead offSELECT 1 AS column1, 'one' AS column2
for example.. Only problem not all databases support theVALUES()
for tableless selects also some database implement it with a dummy table like Oracle databaseDUAL
table, which are works in MySQL and MariaDB.
– Raymond Nijland
Mar 28 at 21:36
|
show 5 more comments
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 aSELECT
without aFROM
. 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 usingSELECT t.column1 , t.column2 FROM ( VALUES(1, 'one') ) AS t (column1, column2)
instead offSELECT 1 AS column1, 'one' AS column2
for example.. Only problem not all databases support theVALUES()
for tableless selects also some database implement it with a dummy table like Oracle databaseDUAL
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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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.
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment
|
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.
add a comment
|
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.
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.
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
add a comment
|
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aFROM
. 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 offSELECT 1 AS column1, 'one' AS column2
for example.. Only problem not all databases support theVALUES()
for tableless selects also some database implement it with a dummy table like Oracle databaseDUAL
table, which are works in MySQL and MariaDB.– Raymond Nijland
Mar 28 at 21:36