Reputation: 11
I just added a WYSIWYG editor on my website so my users can format their content any way they want. The editor works fine and is successfully saving the html coded content on the database.
The problem is when I try to view the content from MSSQL database, here's what it shows on the webpage:
�fa)2016-03-16 12:35:19.000Pg;e)@��+�!!P�P+�fa ��`��,�!H+�H+� �����,�1 � �||�`���X���sql�1 m!���1rsode!!htmlspecialchars_decode!flyerflyer9
���9����X� ������x�����`��1�r~����X�����id!1+!connection.phpflyer_id,� x����,�hpr_id,� x����,�) ����a)���]�� �|����*?���@B�r~�������������Ya�v�&������C:\inetpub\wwwroot\123\test_en_de.phpY43ectsql�9SELECT * FROM flyer_master Where flyer_id = '9����#����)`!�@#��,��,��,��)' ��Errorresultodbc_exec� h����,�
The php code to display the content is:
<?php
include("connection.php");
$flyer_id = 43 ;
$sql = ("SELECT * FROM flyer_master Where flyer_id = '".$flyer_id."' ") or die ("Error");
$result=odbc_exec($connect,$sql)or die(exit("Error en odbc_exec"));
while(odbc_fetch_row($result))
{
$id = odbc_result($result,1);
$name = odbc_result($result,2);
$flyer = odbc_result($result,3);
}
echo $flyer;
//$a = htmlentities($flyer);
echo "<br>";
echo htmlspecialchars_decode(flyer);
//echo $de_code = html_entity_decode($flyer);
//echo $de_code = htmlspecialchars($flyer ,ENT_QUOTES, 'UTF-8');
?>
Can you please help me correct this code so the html coded text is displayed properly? Or, is there another way to do this?
Note i m using php+odbc+Microsoft SQL Server 2008 R2 (RTM).
Upvotes: 1
Views: 607
Reputation: 450
You can use the utf-8 encoding
I needed to access the database from within one particular webhosting service. Pages are UTF-8 encoded and data received by forms should be inserted into database without changing the encoding. The database is also in UTF-8.
Neither SET character set 'utf8' or SET names 'utf8' worked properly here, so this workaround made sure all variables are set to utf-8.
<?php
// ... (creating a connection to mysql) ...
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";} exit;
?>`
All important variables are now utf-8 and we can safely use INSERTs or SELECTs with mysql_escape_string($var) without any encoding functions.
Upvotes: 1