Return to Snippet

Revision: 51994
at October 10, 2011 17:07 by f6design


Updated Code
<?php

$connector = new DbConnector(); // Change to YOUR db connection class.
$query = "SET NAMES 'utf8'"; // Put MySQL connection into UTF8 mode.
$connector->query($query); // Change to YOUR db query execution method.
		
$tables = array('table1','table2'); // Change to YOUR table names.
foreach($tables as $table) 
    {
    	   
        $sql = "SELECT * FROM {$table}";
        $rows = $connector->query($sql); // Change to YOUR db query execution method.
        while($row = mysql_fetch_assoc($rows))
            {
                $new = array();
                foreach($row as $key => $data)
                    {
                        $new[$key] = $connector->escapeString(html_entity_decode($data, ENT_QUOTES, 'UTF-8')); // Change to YOUR db escape execution method.
                    }
                array_shift($new);
                $new_string = "";
                $i = 0;
                foreach($new as $new_key => $new_data)
                    {
                        if($i > 0) { $new_string.= ", "; }
                        $new_string.= $new_key . "='" . $new_data . "'";
                        $i++;
                    }
                $sql = "UPDATE {$table} SET " . $new_string . " WHERE id='" . $row['id'] . "'";
                $connector->query($sql); // Change to YOUR db query execution method.
            }
    }

?>

Revision: 51993
at October 10, 2011 17:06 by f6design


Initial Code
<?php

$connector = new DbConnector(); // Change to YOUR db connection class.
$query = "SET NAMES 'utf8'"; // Put MySQL connection into UTF8 mode.
$connector->query($query); // Change to YOUR db query execution method.
		
$tables = array('table1','table2'); // Change to YOUR table names.
foreach($tables as $table) 
    {
    	   
        $sql = "SELECT * FROM {$table}";
        $rows = $connector->query($sql); // Change to YOUR db query execution method.
        while($row = mysql_fetch_assoc($rows))
            {
                $new = array();
                foreach($row as $key => $data)
                    {
                        $new[$key] = $connector->escapeString(html_entity_decode($data, ENT_QUOTES, 'UTF-8')); // Change to YOUR db escape execution method.
                    }
                array_shift($new);
                $new_string = "";
                $i = 0;
                foreach($new as $new_key => $new_data)
                    {
                        if($i > 0) { $new_string.= ", "; }
                        $new_string.= $new_key . "='" . $new_data . "'";
                        $i++;
                    }
                $sql = "UPDATE {$table} SET " . $new_string . " WHERE id='" . $row['id'] . "'";
                $connector->query($sql); // Change to YOUR db query execution method.
            }
    }
?>

Initial URL
http://stackoverflow.com/questions/2838245/how-to-remove-htmlentities-values-from-the-database

Initial Description
Sometimes a legacy database will have HTML entities stored in it.

This function converts them. Note that this function assumes you have a database abstraction layer, and may need to be modified to connect to YOUR database.

Initial Title
Convert all HTML entities in database to actual characters

Initial Tags
mysql, database, html, convert

Initial Language
PHP