SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting data using prepared statements

    I've switched to using prepared statements to query the database, but I'm running across an issue. I'm trying to insert into a table a simple blog post, which is structured like so:

    Code:
    postID INT (etc),
    postTitle VARCHAR(100),
    postDate DATETIME,
    postBody TEXT
    Looking through the PHP docs, it appears that the only data types that you can bind are ints (i), strings (s), doubles (d), and blobs (b). If I enter text into my textarea for postBody that looks like this:

    Code:
    This is a first line.
    
    This is a second line.
    That's the way it would be stored in the database previously when I was not using prepared statements. Now that I have to cast it as a string, it stores it in the db like so:

    Code:
    This is a first line.\r\n\rThis is a second line.
    Which I don't want. I tried using the blog data type but then nothing was inserted into the db. Is there anyway I can preserve the data's structure integrity like I want to using prepared statements? Or is there no disadvantage to saving data into a db TEXT field in one loooong string line.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Nick View Post
    Code:
    This is a first line.\r\n\rThis is a second line.
    Which I don't want.
    Why don't you want it like that, it's how it should be stored.

    use nl2br() when displaying again
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mark's answer is 100% right.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How it's stored is of no concern to you. All you need to know is what you put in will come back out exactly the same*. If you think this is not the case, then I would say you're probably mistaken about what you're putting in.

    *There's a few exceptions, mainly related to charsets and column types.


Bookmarks

Posting Permissions

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