1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

URLs in SQL?

Discussion in 'MySQL' started by Sanzbar, Feb 1, 2006.

  1. #1
    Hey guys,

    I'm putting together a script that pulls a database for definitions...

    It's very straight forward. Pull the word and definition, which are each stored as fields in the definitions table.

    I would like the definitions to be able to have URLs in them. But when I just add the URL code to the text, it truncates the text where the URL is. Is there an easy way to pull text from SQL and have URLs to the text?

    Thanks,
    Sanzbar
     
    Sanzbar, Feb 1, 2006 IP
  2. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Could you give us an example of the text that's giving you trouble. Do remember that if your text contains HTML code, you will have to use single quotes and not double quotes. Ex:

    Use <a href='http://www.mysite.com'>My Site</a>
    And not <a href="http://www.mysite.com">My Site</a>

    You'll have issues using the second format.
     
    dfsweb, Feb 1, 2006 IP
  3. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    before you put any non numeric value into a database you should be cleaning your data first.

    I use php and mysql, any non numeric value I store I do
    $clean[$value]=mysql_real_escape_string($value);

    that will also take care of any issues with single or double quotes
     
    stuw, Feb 1, 2006 IP
  4. AWD1

    AWD1 Peon

    Messages:
    191
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's not true.

    First of all, it's the single quotes that would cause issues (mostly with SQL injection) not double quotes.

    Second, depending on how your SQL query is formed, you can simply replace each single quote with two sets of single quotes and you're good to go.

    Here's an ASP code snippet I use for inserting into database.
    
    function SQLConvert (Term)
    
         if Term <> "" and not IsNull (Term) then
             Term = Replace (Term, chr (39), chr (39) & chr (39))
             SQLConvert = Term
         end if
    
    end function
    
    Code (markup):
    And then to call it, you do something like this:
    
    Dim URL_Field
    URL_Field = "<a href=""" & Request.Form ("URL") & """>Some URL Text</a>"
    URL_Field = SQLConvert (URL_Field)
    
    (SQL Query here).
    
    Code (markup):
    All single quotes will be replaced with two single quotes, and you're good to go.

    Mind you, in the case of a URL, there should be some form of validation to ensure that the user inputted a proper URL and not just some random thing like "this is a URL, no really, it is, just ignore the commas and spaces."

    As far as retrieving, something like this would work:
    
    Dim URL_Field
    URL_Field = RS ("URL_Field")
    Response.Write "<a href=""" & URL_Field & """>some URL</a>"
    
    Code (markup):
    Any time you want to write double quotes, you just use two sets of them.
     
    AWD1, Feb 1, 2006 IP
  5. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good LORD! I am glad I that I ditched asp years ago - php is sooo much easier to understand
    $clean[$value]=mysql_real_escape_string($value);
    is much shorter to type :)
     
    stuw, Feb 1, 2006 IP
  6. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The single quotes actually worked!!!

    Not sure how or why, but it did. Thanks guys!
     
    Sanzbar, Feb 1, 2006 IP