Writing all mysql results into file?











up vote
-2
down vote

favorite












i am trying to make a program that syncs all the outcome of a row in my mysql database to a .txt file. But it doesnt work. I want each result on a new line.



This is my code:



<?php
$db = mysqli_connect("localhost", "username", "pass", "database");


$hwid_query = "SELECT hwid FROM users";
$result = mysqli_query($db, $hwid_query);
//$hwids = mysqli_fetch_assoc($result);


while($row = mysqli_fetch_assoc($result)){
$hwids = $row['hwid'];
echo $hwids;
}


$fp = fopen('lt.txt', 'w');
ftruncate($fp, 0);
fwrite($fp, $hwids);
fclose($fp);
mysqli_close($db);
?>


This results in one line in the file with only one of the columns of the database, while echo $hwids; gives all of them. What am i doing wrong?










share|improve this question
























  • The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
    – Martijn
    Nov 23 at 8:12















up vote
-2
down vote

favorite












i am trying to make a program that syncs all the outcome of a row in my mysql database to a .txt file. But it doesnt work. I want each result on a new line.



This is my code:



<?php
$db = mysqli_connect("localhost", "username", "pass", "database");


$hwid_query = "SELECT hwid FROM users";
$result = mysqli_query($db, $hwid_query);
//$hwids = mysqli_fetch_assoc($result);


while($row = mysqli_fetch_assoc($result)){
$hwids = $row['hwid'];
echo $hwids;
}


$fp = fopen('lt.txt', 'w');
ftruncate($fp, 0);
fwrite($fp, $hwids);
fclose($fp);
mysqli_close($db);
?>


This results in one line in the file with only one of the columns of the database, while echo $hwids; gives all of them. What am i doing wrong?










share|improve this question
























  • The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
    – Martijn
    Nov 23 at 8:12













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











i am trying to make a program that syncs all the outcome of a row in my mysql database to a .txt file. But it doesnt work. I want each result on a new line.



This is my code:



<?php
$db = mysqli_connect("localhost", "username", "pass", "database");


$hwid_query = "SELECT hwid FROM users";
$result = mysqli_query($db, $hwid_query);
//$hwids = mysqli_fetch_assoc($result);


while($row = mysqli_fetch_assoc($result)){
$hwids = $row['hwid'];
echo $hwids;
}


$fp = fopen('lt.txt', 'w');
ftruncate($fp, 0);
fwrite($fp, $hwids);
fclose($fp);
mysqli_close($db);
?>


This results in one line in the file with only one of the columns of the database, while echo $hwids; gives all of them. What am i doing wrong?










share|improve this question















i am trying to make a program that syncs all the outcome of a row in my mysql database to a .txt file. But it doesnt work. I want each result on a new line.



This is my code:



<?php
$db = mysqli_connect("localhost", "username", "pass", "database");


$hwid_query = "SELECT hwid FROM users";
$result = mysqli_query($db, $hwid_query);
//$hwids = mysqli_fetch_assoc($result);


while($row = mysqli_fetch_assoc($result)){
$hwids = $row['hwid'];
echo $hwids;
}


$fp = fopen('lt.txt', 'w');
ftruncate($fp, 0);
fwrite($fp, $hwids);
fclose($fp);
mysqli_close($db);
?>


This results in one line in the file with only one of the columns of the database, while echo $hwids; gives all of them. What am i doing wrong?







php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 10:40









legoscia

28.6k1179110




28.6k1179110










asked Nov 22 at 10:35









Nonstop Youtuber

27




27












  • The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
    – Martijn
    Nov 23 at 8:12


















  • The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
    – Martijn
    Nov 23 at 8:12
















The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
– Martijn
Nov 23 at 8:12




The reason you get downvotes is because you miss the "What have you tried?" part. Try to figure out why it doesnt work. This is a difficult to learn skill, but after practising a valueable one as it helps debugging later on in your progress.
– Martijn
Nov 23 at 8:12












1 Answer
1






active

oldest

votes

















up vote
2
down vote













In the while loop you set $hwids to that rows value. The next itteration (eg the next row from the DB) overwrites that same $hwids to it's value, it overwrites it. At the end of the while, you have the last entry from your result.



There are a few solutions, but in the one below, I write the value to the file per itteration:



$file = fopen('lt.txt', 'wb');
while($row = mysqli_fetch_assoc($result)){
$lineToAdd = $row['hwid'] . PHP_EOL; // PHP_EOL = "n" = Newline
fwrite($file, $lineToAdd);
}
fclose($file);


I've based this upon this answer






share|improve this answer























  • Thanks! Why does everyone disrate my questions?
    – Nonstop Youtuber
    Nov 23 at 8:08










  • I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
    – Martijn
    Nov 23 at 8:13











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',
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/3.0/"u003ecc by-sa 3.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%2f53429000%2fwriting-all-mysql-results-into-file%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








up vote
2
down vote













In the while loop you set $hwids to that rows value. The next itteration (eg the next row from the DB) overwrites that same $hwids to it's value, it overwrites it. At the end of the while, you have the last entry from your result.



There are a few solutions, but in the one below, I write the value to the file per itteration:



$file = fopen('lt.txt', 'wb');
while($row = mysqli_fetch_assoc($result)){
$lineToAdd = $row['hwid'] . PHP_EOL; // PHP_EOL = "n" = Newline
fwrite($file, $lineToAdd);
}
fclose($file);


I've based this upon this answer






share|improve this answer























  • Thanks! Why does everyone disrate my questions?
    – Nonstop Youtuber
    Nov 23 at 8:08










  • I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
    – Martijn
    Nov 23 at 8:13















up vote
2
down vote













In the while loop you set $hwids to that rows value. The next itteration (eg the next row from the DB) overwrites that same $hwids to it's value, it overwrites it. At the end of the while, you have the last entry from your result.



There are a few solutions, but in the one below, I write the value to the file per itteration:



$file = fopen('lt.txt', 'wb');
while($row = mysqli_fetch_assoc($result)){
$lineToAdd = $row['hwid'] . PHP_EOL; // PHP_EOL = "n" = Newline
fwrite($file, $lineToAdd);
}
fclose($file);


I've based this upon this answer






share|improve this answer























  • Thanks! Why does everyone disrate my questions?
    – Nonstop Youtuber
    Nov 23 at 8:08










  • I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
    – Martijn
    Nov 23 at 8:13













up vote
2
down vote










up vote
2
down vote









In the while loop you set $hwids to that rows value. The next itteration (eg the next row from the DB) overwrites that same $hwids to it's value, it overwrites it. At the end of the while, you have the last entry from your result.



There are a few solutions, but in the one below, I write the value to the file per itteration:



$file = fopen('lt.txt', 'wb');
while($row = mysqli_fetch_assoc($result)){
$lineToAdd = $row['hwid'] . PHP_EOL; // PHP_EOL = "n" = Newline
fwrite($file, $lineToAdd);
}
fclose($file);


I've based this upon this answer






share|improve this answer














In the while loop you set $hwids to that rows value. The next itteration (eg the next row from the DB) overwrites that same $hwids to it's value, it overwrites it. At the end of the while, you have the last entry from your result.



There are a few solutions, but in the one below, I write the value to the file per itteration:



$file = fopen('lt.txt', 'wb');
while($row = mysqli_fetch_assoc($result)){
$lineToAdd = $row['hwid'] . PHP_EOL; // PHP_EOL = "n" = Newline
fwrite($file, $lineToAdd);
}
fclose($file);


I've based this upon this answer







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 10:51

























answered Nov 22 at 10:41









Martijn

11.8k31954




11.8k31954












  • Thanks! Why does everyone disrate my questions?
    – Nonstop Youtuber
    Nov 23 at 8:08










  • I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
    – Martijn
    Nov 23 at 8:13


















  • Thanks! Why does everyone disrate my questions?
    – Nonstop Youtuber
    Nov 23 at 8:08










  • I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
    – Martijn
    Nov 23 at 8:13
















Thanks! Why does everyone disrate my questions?
– Nonstop Youtuber
Nov 23 at 8:08




Thanks! Why does everyone disrate my questions?
– Nonstop Youtuber
Nov 23 at 8:08












I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
– Martijn
Nov 23 at 8:13




I've answered that under your question. On this site, when an posted answer is the solution to a problem, it's normal to mark it as such, so that other people can easily spot the solution when they have the same issue.
– Martijn
Nov 23 at 8:13


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53429000%2fwriting-all-mysql-results-into-file%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu