Results 1 to 5 of 5

Thread: Formatting data being written to MySQL...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Question Formatting data being written to MySQL...

    Hopefully the last question

    Two issues

    1. Have a text area where the operator can enter whatever and it then gets written to the database. Unfort it's not including paragraph breaks Is there a function in PHP to replace line breaks with <br /> or something similar.

    2. Names entered as O'Brian are appearing as O\'Brian when read from the database, does PHP have a character escape function, cause I guess that's what I need.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Formatting data being written to MySQL...

    1. you could use nl2br(). this replaces every new line to an HTML line break. alternatively, and mostly just because I have a thing against "<br />", I do this:
    PHP Code:
        $body "<p>" $body "</p>";
        
    $body str_replace("\n\r""</p><p>"$body);
        
    $body str_replace("\n"""$body);    
        
    $body str_replace("\r"""$body); 
    2. use stripslashes() on any POST/GET variables when you have magic_quotes_gpc on before entering them into your queries.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Formatting data being written to MySQL...

    kows,

    Just to verify.

    I use both methods before writing the data away to the database or after retrieving it from the database?

    Thanks for the help btw, the stupid software wont allow me to add to your rep

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Formatting data being written to MySQL...

    didn't get to check this until just now.

    I generally store all information in the database as plain text, and apply any line-breaking methods when displaying it. This makes it easy to be used in different situations, if those situations ever occur. I think that some forum softwares keep a plain text version and an HTML version in the database, so that extra processing is not needed every time they want to display the information. if you want, you could do that, too.

    for inserting to the database, I always make a variable calling mysql_real_escape_string() to make sure there are slashes where there needs to be. if you're displaying the information and still have slashes, you can use stripslashes() when you're displaying it.
    PHP Code:
    <?php
      $var_to_insert 
    mysql_real_escape_string($_POST['var']);
    ?>



    <?php
      $var_to_display 
    stripslashes($result['var']);
      echo 
    $var_to_display;
    ?>

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Formatting data being written to MySQL...

    Cool, off to use my new knowledge to combat the forces of darkness

    Thanks once again kows

Posting Permissions

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



Click Here to Expand Forum to Full Width