encode & decode html code from mssql in php - php

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).

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.

Related

Store and output text with accents

I have some text in a database. I use French and English. French has accents, and some special characters like ç. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci (the default). I tried utf8_general_ci and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
Why htmlentities does not work?
Is utf8_encode(); the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is already UTF8? Is there any better way to store and output text with accents in a MySQL database?
After you connect to the DB you should set the client charset to UTF8:
mysqli_set_charset($con, "UTF8");
Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).
Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)

Polish characters in mysql response

I have a problem with polish characters. I can't get correctly written words, like '?ukasz' instead of "Łukasz" or even "null", when it supposed to be "Kraków". I tried "mysql_set_charset('utf-8'/'iso-8859-1')" after mysql_connect or iconv(on json_encode($output)) and it's still the same (except now there is "Krak\u00f3" instead of "null"). I'll appreciate any help.
This is a php file for my Android app:
$id_client = $_REQUEST['id_klienta'];
$con=mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('courier_helper') or die(mysql_error());
$sql=mysql_query("SELECT * FROM `clients` WHERE id_klienta='$id_client'");
while($r=mysql_fetch_assoc($sql))
$output[]=$r;
print(json_encode($output));
mysql_close($con);
?>
You have to make sure, that you are using UTF-8 everywhere:
script file encoding (UTF-8 instead of ANSI) - you can set encoding it in Notepad++
html code (meta charset)
database table charset (when you are creating table or database)
database mysql_set_charset('utf8', $connection_obj);
database SET NAMES utf8 - run that SQL command after connecting
And one more thing - get familiar with PDO. This is my PDO connect function I use:
function DbConnect()
{
$db_host = "localhost";
$db_name = "database_name";
$db_user = "your_username";
$db_pass = "your_passwd";
$link = new PDO("mysql:host=$db_host;dbname=$db_name; charset=UTF-8", $db_user, $db_pass);
$link->exec("set names utf8;");
return $link;
}
You can use that function like this (this is PDO example):
$link = DbConnect();
$query = $link->prepare("SELECT id FROM wp_users");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You should have your database storing data as UTF8, which means converting your existing tables.
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
You also need to make sure your connection to the database is UTF8. You can make sure of that by running a SET NAMES query right after your connect.
SET NAMES UTF8
As others mentioned, you should start using PDO.

How to display Germany Umlauts (ü, ä, ö) from sql in Php?

I am trying to display this German character the proper way, if I don't use htmlentities() the output is unabh�ngig, but when I try to use htmlentities() the word is missing, why is that?
<?php
$str = htmlentities("unabhängig");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset='utf-8'>
</head>
<body>
</body>
<div><?php echo $str;?></div>
I had the same problem with the ü, ä, etc.
Right after selecting the database I implemented this two lines:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
as suggested here:
http://www.flashhilfe.de/forum/serverseitige-programmierung/php-mysql-wie-utf-8-beim-connect-festlegen-225225-225225.html
Best, Yannis
You should just be able to send ü without encoding it separately by ensuring that the charset is set to utf-8 using HTTP header.
If for some reason you can't do that, use:
Ü = Ü
or
ü = ü
This is the format for charset:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Make sure your encoding is not being forced by htaccess or apache. check what is the selected encoding in your browser when you view the site, if it is not utf-8, then it is being forced. If forced you can add this to htaccess
AddDefaultCharset UTF-8
Make sure your database table is in utf-8
Make sure your connection to your database is in utf-8 http://php.net/manual/en/function.mysql-set-charset.php
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
You can pass UTF-8 encoding to htmlentities to make it use correct encoding. or you can use htmlspecialchars http://php.net/manual/en/function.htmlentities.php
And if you don't want to worry about so many different charset codings or if htmlentities doesn't work for you, here the alternative:
I used mysqli DB connection (and PHPV5) Form post for writing/inserting to MySQl DB.
$Notes = $_POST['Notes']; //can be text input or textarea.
$Notes = str_replace('"', '"', $Notes); //double quotes for mailto: emails.
$von = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é"); //to correct double whitepaces as well
$zu = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é");
$Notes = str_replace($von, $zu, $Notes);
echo " Notes:".$Notes."<br>" ;
$Notes = mysqli_real_escape_string($link, $Notes); //for recommended mysqli DB connection.
//$Notes = mysql_real_escape_string($Notes); //for old deprecated mysql DB connection.
// mysqli_real_escape_string Escapes special characters in a string for use in an SQL statement
echo " Notes:".$Notes ; //ready for inserting
enter code here
//Optional:
$charset = mysqli_character_set_name($link); //only works for mysqli DB connection
printf ("To check your character set but not necessary %s\n",$charset);

How to manage ID in database with Japanese characters

Now I got a database. The database is encoded with the almighty utf-8 collation. Actually collation is utf8, I am not sure what the encoding is. That should be another question.
Then I made a program to retrieve data from the database.
<?php
require_once('convertArraytoJson.php');
require_once('config.php');
mysql_connect ( "localhost", $databaseuser, $databasepassword);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
#mysql_select_db ($databasename) or die ( "Unable to select database" );
$data=$_GET['id'];
$query="SELECT * FROM `tabletes` where id = '".$data."'";
$data = mysql_query($query);
while (true){
$info = mysql_fetch_array ( $data, MYSQL_ASSOC );
if ($info == false) {
break;
}
//$output[]=$info;
$output[$info['ID']]=$info;
unset ($output[$info['ID']]['ID']);
}
$result = array2json($output);
echo $result;
?>
The content of the database looks like this:
Now I call the function by doing this (you need to enlarge your screen to see it):
http://localhost/domainname/api/test2.php?id=jr-東北本線-荒川橋梁__35.79_139.72
It doesn't work.
However, if I do NOT use $_GET but simply enter the Japanese characters directly in the code it works.
So if I change:
$data = $_GET['id']
to
$data = 'jr-東北本線-荒川橋梁__35.79_139.72'
Things are working fine.
Of course, I don't want to hardwire the ID, I want to access that via $_GET['id']. What should I do?
Just use urlencode() - your string becomes like:
%E6%9D%B1%E5%8C%97%E6%9C%AC%E7%B7%9A-%E8%8D%92%E5%B7%9D%E6%A9%8B%E6%A2%81.
These Chinese characters are not allowed.
Check rfc1738: Uniform Resource Locators (URL)
You can not use Japanese characters directly into the url using utf-8. You need to use url encoding to pass the parameter.
Look at the following link too.
Japanese characters in URL/FTP server

How to resolve mysql text encoding issue

I'm struggling with a mysql import issue and the usual remedies don't seem to be working. I'm trying to copy fields from one database to another (both Drupal systems).
Running "show table status on the databases" I notice that the origin table is utf8_bin and the destination table is utf8_general_ci.
I'm currently doing the import like this:
$olddb = mysql_connect("localhost", "user", "password");
mysql_select_db("origin", $olddb);
$result = mysql_query("set names utf8", $olddb);
$newdb = mysql_connect("localhost", "user", "password");
mysql_select_db("destination", $newdb);
$result = mysql_query("set names utf8", $newdb);
$result = mysql_query("select first_name from origin_table", $olddb);
$row = mysql_fetch_array($result);
$query = "update destination_table set first_name = \"".$row["first_name"]."\"";
$result = mysql_query($query, $olddb);
The text looks like its importing correctly but when I try and edit the same fields in Drupal, I get the following weird question-mark characters between every character.
Fields in Safari browser
Fields in Firefox browser
Any ideas?
How are you outputting the data from PHP->browser? You'd need to issue the appropriate headers to tell the browser to expect UTF-8 text, e.g:
header("Content-Type: text/plain; charset=utf-8");
Given the "weird" characters, I'm guessing FF is seeing the text as something non-unicode, like iso8859-1 and assuming 1 byte/character.

Categories