>>> Programming >> PHP > How do I extract some data from a database and save it to a text file? (This page has been seen 862 times)
How do I extract some data from a database and save it to a text file?
Let say you have a Table called Users, and you wanna save each username from that table to a text file. You could do something like this.
$select = "SELECT * FROM Users";
$rs = $conn->execute($select);
$Content = "";
while(!$rs->EOF){
$Content .= "
".$rs->Fields['PersonName']->value.";
";
$rs->MoveNext();
}
$file = fopen("c:\\php\\data.txt", "a+");
fwrite($file, $Content);
What we do here is that we put a ; behind $rs->Fields['PersonName']->value, That makes it easier for us if we wanna use the data somewhere else. Lets say you wanna import it into Excel or back into sql server, then you would just say that the delimiter is a ; and then it knows when to split the string.
Another thing we are doing here is using a+ in the fopen command, this means that it is appending information to the data.txt file, if we just made a "w" instead of "a+" then we would overwrite the file everytime.
Remember that PHP needs to have write and read permission to the directory you are saving the file to, in this case its c:\php
$select = "SELECT * FROM Users";
$rs = $conn->execute($select);
$Content = "";
while(!$rs->EOF){
$Content .= "
".$rs->Fields['PersonName']->value.";
";
$rs->MoveNext();
}
$file = fopen("c:\\php\\data.txt", "a+");
fwrite($file, $Content);
What we do here is that we put a ; behind $rs->Fields['PersonName']->value, That makes it easier for us if we wanna use the data somewhere else. Lets say you wanna import it into Excel or back into sql server, then you would just say that the delimiter is a ; and then it knows when to split the string.
Another thing we are doing here is using a+ in the fopen command, this means that it is appending information to the data.txt file, if we just made a "w" instead of "a+" then we would overwrite the file everytime.
Remember that PHP needs to have write and read permission to the directory you are saving the file to, in this case its c:\php
Like (0)
Dislike (1)
Keywords for this article:
SAVE || PHP || FILE
Advertisement by Google
Comment:
Code Language:
Code:
Here you can paste a code example. It will then be processed by SyntaxHighlighter and formatted for easier readability.
Please remember to select the correct Code Language in the select above so the SyntaxHighlighter can highlight the code properly.
Code:
Please enter the code you see above
What is 6 + 8 =