1

I am trying to create a text file using php and the value is coming through a db call

$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
$x1=$row{'code'};
exec("printf $x1 >> code.txt");
}

But the value is not inserted in the code.txt. Only array(code) is inserted in the code.txt. Actually the $row{'code'} has "#xy { } () %". How can i write the values to txt file.

6 Answers 6

3

This will solve your problem:

$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
file_put_contents('code.txt',$row['code'],FILE_APPEND);//FILE_APPEND is necessary
}
Sign up to request clarification or add additional context in comments.

5 Comments

Same data will append in every refresh.
@Salim no , data change in each iteration,and File_APPEND put cursor in the end of file so data are not erased
$x1="abc "; file_put_contents('code.txt',$x1, FILE_APPEND); If you refresh page 3 times output will be 'abc abc abc'
@Salim OP have not said anything about this.He should specify what he wants,don't forget that database is not a variable and values change
no needs for $x1=$row['code']; every cycle. Just file_put_contents('code.txt', $row['code'], FILE_APPEND);. Mysql deprecated. Cyclic file access instead one save...
1

Use file_put_contents instead of that exec and do

$s = "";
while ($row = mysql_fetch_array($result)) {
    $x1=$row['code'];
    $s .= $x1 . "\n";
}
file_put_contents("code.txt", $s, FILE_APPEND);

7 Comments

WIth the FILE_APPEND flag to not erase the previous data
i think \r\n is better
@FaceOfJock Using \r\n in Linux will produce undesirable results. This works fine for Windows, but not Linux.
@Fred-ii- but in notepad++ for example , \n will not pass to next line
@FaceOfJock What do you by "will not pass to next line"?
|
0

First of all, you shouldn't use $row{'code'} the correct usage is $row['code']

Also, I would suggest using file_put_contents() instead of exec so you could do

$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
$x1=$row['code'];
file_put_contents('code.txt',$x1, FILE_APPEND);
}

4 Comments

FILE_APPEND should be in file_input or content will be errased and only last line will be in the file
I think it will append data for every refresh of this page. same data will store every time
$x1="abc "; file_put_contents('code.txt',$x1, FILE_APPEND); If you refresh page 3 time output will be 'abc abc abc' I think he did not want this
See the answer code.txt will contain amount of data which is in database in every refresh stackoverflow.com/a/18642996/2459296
0

Actually, you are not creating a text file using PHP, you are creating a text file using shell execution. Why don’t you just try file functions?

$result = mysql_query("SELECT code FROM topic_master");
$file = fopen("code.txt", "ab");
while ($row = mysql_fetch_array($result)) {
    fwrite($file, $row["code"]);
}
fclose($file);

2 Comments

"ab"? b is for binary. Either a or a+ if it's text, not the b switch.
PHP manual says, among other things: “For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().”
0

Get all data in a variable and write it in file

<?php 

$result = mysql_query("SELECT code FROM topic_master");

$data = "";
while ($row = mysql_fetch_array($result)) {

    $x1=$row['code'];

    $data .= $x1;

}

$fp = fopen("code.txt","w");

fwrite($fp,$data);

fclose($fp);

?> 

5 Comments

fwrite($fp,$data) instead of fwrite($fp,$x1) and also $fp = fopen("code.txt","a"); to place pointer in end of file not in the beginning
@Salim w will overwrite content. OP needs to append, therefore a or a+
It is out of loop and all value stored in variable $data it is writing to file only once
@Salim As per OP's title "Using php creating file and adding text to it" See accepted answer, where it uses FILE_APPEND
Read comments in excepted answer
0

$result = mysql_query("SELECT code FROM topic_master");

// if you want to APPEND the data in code.txt, use this
// however, using that particular SQL query the same data will be written over and over because there's nothing specifying a parameter change
while ($row = mysql_fetch_array($result)) {
    $x1 = $row['code'];
    write_file('code.txt',$x1,'a');
}

// if you want to OVERWRITE the data in code.txt use this
while ($row = mysql_fetch_array($result)) {
    $x1 .= $row['code'];
}
write_file('code.txt',$x1,'w');


// function to write content to a file (with error checking)
function write_file($filename,$content,$flag) {
    if(is_writable($filename)) {
        if(!$handle = fopen($filename, $flag)) {
            // die, or (preferably) write to error handler function
            die('error');
        }
        if(fwrite($handle, $content) === FALSE) {
            // die, or (preferably) write to error handler function
            die('error');
        }
        fclose($handle);
    }
}

Edit: changed flag from w to a.

Another edit: if you want to APPEND to the file, leave the fopen() flag set to 'a'. If you want to overwrite the existing contents, change the flag to 'w'.

Final edit: added two versions with explanation

6 Comments

w will overwrite content. OP needs to append, therefore a or a+
Thanks, didn't see that. Where does he state he needs to append?
You're welcome Tim. In the title "Using php creating file and adding text to it"
Yeah, it's not always clear when the important stuff is in the title and not the question body itself; an honest mistake ;-)
Totally confusing regardless, because "adding" can mean both. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.