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?
php mysql
add a comment |
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?
php mysql
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
add a comment |
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?
php mysql
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
php mysql
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
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.
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.
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%2f53429000%2fwriting-all-mysql-results-into-file%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
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