Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Write data from DB to txt file

    Hey. I have a question that may be simple to some but has confused me.

    I have a script that queries a database and pulls a list of names out of the table. The query is fine i have it echoed on my page above were the txt file is included so i can compare the two results

    I am trying to get it so that when i submit a form on the page. it writes the values of the db query into the file. so far i have had only success... just briefly with writing to the end of the file so it doesnt erase what it has already written in that session.

    what i want basically is for the content of my bd query to be written to a txt file so the txt file is updated whenever i submit the form. also if possible id like the script to create the file if it isnt already there.

    so far i have:

    PHP Code:
    if ($_POST['Submit'])
    {
            
    // if the button is pressed
                
        // write to the file
        
    $fp fopen"names.txt" "a+" );

        while (
    $i $num)
        {
            
    $name mysql_result($result,$i,"name");
            
    fwrite$fp$name );
            
    $i++;
        }

        
    fclose$fp );
    }
    else
    {
        
    // do nothing

    $i is declared above and is used to compare the number of rows with the counter.

    any help would be great. thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    From what I see, you don't have enough information to correctly do this. However, you mentioned that it works fine so I'm no concerned about your generation.
    In order to overwrite your current text file, you need to use 'w+' as your mode, as it will truncate the current file and put the file pointer to the beginning. 'a+' will point to the end. Also, I'd recommend using a 'b' at the end of your mode for compatilbility with windows, and remember your linebreaks are different depending on your server OS - \n for *nix, \r\n for windows, and \r for mac.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, fopen will automagically attempt to create the file (with most modes, including w+) if the file doesn't exist and it has permission. Just make sure you chmod the directory first so the file can be created. Of course, you could use php to set the permissions for you for the duration of the script then change them back afterwards for security, but you probably ought to just get it working first before you go there.

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for your replies and suggestions, will take them into consideration. I figured out why it wasn't working. I had been using $i for an above loop so all i needed to do was declare a new var $j and loop through the data using that var instead.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •