using php -- save html in mysql DB

Discussion in 'PHP' started by akashif, Jan 31, 2007.

  1. #1
    hi

    i have a big database with lots of customers comments. i want to copy all the data to a new database [changing the software and importing old data]

    customers coments contains HTML as well and special cheracters as well

    Example 1
    Example 2

    when i select data from database using php and try to save it in new DB
    query often gets terminated due to special cheractors

    
    
    $query="insert into comments (id,custkey,date,cdata) values('$id','$custkey','$NowDate','$oldDBcomments')";
    mysql_query($query,$link);
    
    PHP:
    My main question you can say is

    how can i save data that contains special cheracters like ' , & , " , / , \ in mysql database using php

    my main goal is to copy customers comments from one database to an other . customers comments contains HTML as well

    thanks in advance
     
    If someone posts a solution, use the "Best Answer" link in their post to pick it as the best answer.
    akashif, Jan 31, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    mysql_escape_string()
    PHP:
    htmlentities()
    PHP:
     
    rodney88, Jan 31, 2007 Set Best Answer IP
  3. toll

    toll Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    htmlentities
    (PHP 3, PHP 4, PHP 5)

    htmlentities -- Convert all applicable characters to HTML entities
    Description
    string htmlentities ( string string [, int quote_style [, string charset]] )


    This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

    Like htmlspecialchars(), the optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT:
     
    toll, Jan 31, 2007 Set Best Answer IP
  4. toll

    toll Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    mysql_escape_string
    (PHP 4 >= 4.0.3, PHP 5)

    mysql_escape_string -- Escapes a string for use in a mysql_query
    Description
    string mysql_escape_string ( string unescaped_string )


    This function will escape the unescaped_string, so that it is safe to place it in a mysql_query(). This function is deprecated.

    This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.
     
    toll, Jan 31, 2007 Set Best Answer IP
  5. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Use a function called mysql_real_escape_string() like this
    
    $query="insert into comments (id,custkey,date,cdata) values('" . mysql_real_escape_string($id) . "','" . mysql_real_escape_string($custkey) . "','" . mysql_real_escape_string($NowDate) . "','" . mysql_real_escape_string($oldDBcomments) . "')";
    mysql_query($query,$link);
    Code (markup):
    HTH, cheers! :)
     
    picouli, Jan 31, 2007 Set Best Answer IP