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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Single quotes in database table

    The age old problem and question.
    In my database I have some fields that have single quotes.
    Such as the word it's.

    Using "insert into" and strings for the values, one must quote the stings.
    Values('$string').
    Using values("$string") causes the field to contain the data as $string. Not the desired string.

    As I pull the data from one table, I may have something like "It's late".
    This causes the actual value to show -> Values('it's late').
    This is what generates the error because of the mismatch of quotes.
    I tried using addslashes(), that changed nothing.
    I tried manually editing the database, no change.

    So what is the proper solution to this?

  • #2
    Regular Coder
    Join Date
    Jun 2012
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Solved the problem.
    Using str_replace() works just fine.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by Suwannee_guy View Post
    So what is the proper solution to this?
    one proper solution is using Prepared Statements. they separate SQL statement and Data and hence it does not matter what or how many apostrophes are in your text.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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