UTF-8 PHP SQL SERVER - php

I am working using wordpress, php and sql server. On my web page I have a sqlserver query that is not being displayed correctly.
problem
I am not sure of what should I insert on my php or html code to fix this, I have been trying different things but nothing is fixing my problem. I guess it's all about ut8 encoding but I don't know what to do anymore.
This is how I do the query.
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
$stmt=$dbh->prepare("SELECT TOP 10 till.code......
$stmt->execute();

Try this:
new PDO ("dblib:host=$hostname;dbname=$dbname;charset=utf8","$username","$pw");

Probably it's a problem in HTML generated by Wordpress.
Can you check the meta tags in your HTML?
For HTML4:
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
For HTML5:
<meta charset="UTF-8">
You can find further information on charsets here:
http://www.w3schools.com/html/html_charset.asp
Furthermore it is available in PHP a function able to convert in HTML encoded strings some special characters; you could try to pass columns with problems through the htmlentities function:
http://php.net/manual/en/function.htmlentities.php

Related

How to retrive UTF-8 Data from MSSQL via PHP to HTML

how can i get the UTF-8 data from the MSSQL Table. I have some names that are wrong converted. In the place where the umlat mark is I'm getting a sign like this �.
So for a name Gergö it show's Gerg�.
I'm not sure where the problem is. From the MSSQL to PHP conversion or PHP to HTML conversion. For this purpose im using the sqlsrv functions.
In HTML i added the meta tag <meta http-equiv="content-type" content="text/html; charset=UTF-8"> but this didn't solve the problem. I also added before the connection starts the ini_set('mssql.charset', 'UTF-8'); function but this also didn't helped.

words with accents appear with strange chars in mysql

Information that I send to mysql with accents are appearing with strange chars, for example správce is admin in my language. And when I send this to mysql it appears like "správce".
Im trying to find information to solve this problem, and I saw two solutions, but any is working.
1st solution with meta tags, dont works:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
2º solution with htmlspecialchars method also dont works
if($f['level_admin'] == '1') { $f['level_admin'] = htmlspecialchars('Správce', ENT_QUOTES, "UTF-8"); }
if($f['level_admin'] == '2') { $f['level_admin'] = htmlspecialchars('Super Správce', ENT_QUOTES, "UTF-8");}
Do you know some way that work effectively?
It's also important to know what collation is set in the MySQL DB Table - dependent on your needs you could use for example "utf8_unicode_ci" .
There is also a php function that converts string to UTF8
utf8_decode()
utf8_encode()
Normally this helps - but you better check the collation in the DB.

PHP 5.4 multipart/form-data UTF-8 encoding

I'm having problem with UTF-8 encoding while posting form data as "multipart/form-data", without multipart/form-data everything works well. But since I have to upload files on same post, I need to use multipart/form-data.
Problem started after upgrading from PHP 5.3.x to PHP 5.4.4-14 (bundled with Debian Wheezy), same scripts works well with PHP 5.3 test server.
All of my documents are saved in UTF-8 and has <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> tags.
I tested with different browsers on different computers
mb_detect_encoding() detects posted string as UTF-8
I tried AddDefaultCharset utf-8 for Apache configuration.
Here you can test my scripts, you can copy/paste following string with Turkish characters (ex. string: öçşipğopüp )
http://sa.chelona.com.tr/haber-ekle.html
I also found related question at UTF-8 text is garbled when form is posted as multipart/form-data in PHP but it recommends re-installing apache/php and that's not possible for my situation. Is this a known PHP/Apache bug?
Do a simple conversion from UTF-8 to Turkish Alphabet ISO-8859-9 and the problem should be solved
iconv('UTF-8', "ISO-8859-9", $string);
Example Input : öçşipğopüp
Example Form:
<form method="post" enctype="multipart/form-data" action ="self.php">
<input type="text" name="hello" />
<input type="submit" name="test" />
</form>
Simple Sump :
var_dump($_POST['hello'],iconv('UTF-8', "ISO-8859-9", $_POST['hello']));
Output
string 'öçşipğopüp ' (length=16)
string 'öçþipðopüp ' (length=11)
I'm writing this to answer my own question... I hope it will help somebody else...
if you use PHP 5.4.x, setting mbstring.http_input from "auto" to "pass" may solve your problem.
My php version is 5.4.45 and changing mbstring.http_input from auto to pass works very well. In php.ini file the default value is pass. For more detail about this variable you can see here.
you should to try to re-install your wamp or xampp or your apache and php.and run your code on some one else's machine with the same php version .if this code runs then try to figure out why it is not working in your server or check of file_upload extension in your php.
if uncommenting the default charset line in php.ini does something, ot will be easy to fix.
remember to bounce apache after changing.
I don't think you should be using mb_detect_encoding to determine the encoding in this case.
If you must use it, then maybe you need to set the detection order to make sure UTF-8 is higher up the list, see http://www.php.net/manual/en/function.mb-detect-order.php
You've set the form's accept-charset to UTF-8; you've set the original page to UTF-8: all current browsers will send UTF-8. HTML 5 specifies this FWIW: http://www.w3.org/TR/2011/WD-html5-20110405/association-of-controls-and-forms.html#multipart-form-data
The string will be UTF-8, don't attempt any conversion of it, and you will be fine.
But if you post some of your PHP code then maybe it will be clear what you're trying to do and what's going wrong...
Sorry this is more of an idea for a workaround than actual solution, however if all traditional methods have failed, and you can't reinstall anything, try converting from the UTF8 code points. Something like using a base64 encoding before sending and then decode on receive. Or convert to a hex string and decode after receiving.
You need add headers in PHP and HTML, like lowercase:
<?php header('content-type: text/html; charset=utf-8'); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="post" enctype="multipart/form-data" action ="self.php">
...
</form>
</body>
</html>
Remember: Save all php and html files in utf-8 Without BOM.
Your example page looks correct and the steps you have taken seem to cover most of the important points, there is one more thing i would check though. You wrote that the data is stored in a MySql database with UTF-8 charset, but this doesn't necessarily mean, that the PHP connection object works with this charset too.
// tells the mysqli connection to deliver UTF-8 encoded strings.
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');
// tells the pdo connection to deliver UTF-8 encoded strings.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$db = new PDO($dsn, $dbUser, $dbPassword);
The examples above show how to set the charset for SQLI or PDO. Preparing the connection object this way, makes you independent of the database configuration, if necessary the connection will even convert the returned/sent data.
To test this in your page, make sure that the charset is set, before inserting/querying the database.
mb_internal_encoding("UTF-8");
Add this code before your string..
After a long time trying with unpack() and the proposals from the answers here, I found a pitfall, and maybe you have the same reason for the encoding problem.
All I had to do was making htmlentities using utf-8 explicitly:
htmlentities(stripslashes(trim(rtrim($_POST['title']))), ENT_COMPAT, "utf-8");
This is for php 5.2.xx

Character set encoding issue

All, Im having the age old problem with character encoding...
I have a mySQL DB with a field set to utf8_unicode_ci. My PHP page as the header entry <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />. When I use a simple form to POST data with Cyrillic characters to the DB, e.g. 'гыдлпоо', the characters display correctly in the textarea, and are added to the DB where they display correctly.
When fetching the characters from the DB, my page only displays a series of question marks. I've used mb_detect_encoding($content, "UTF-8,ISO-8859-1", true); and the content is UTF-8, however the characters do not display.
I've searched around (including on SO) and tried any number of solutions, to no avail- any help would be much appreciated.
Many thanks
Do this right after mysql_connect() and mysql_select_db():
mysql_query("SET NAMES 'utf8'");
Try using mysql_set_charset() function before fetching data from database.
did you try to use the form with
enctype="multipart/form-data"
?
this might help.. it's not necessary for the text to be readable in your database.. when they are saved they should be utf8 encoded.. you need them to look fine when you output the string again

undocumented CHARSET built-in variable in php?

In the osCommerce PHP sources in every script there is a line like this:
<meta http-equiv="Content-Type"
content="text/html; charset=<?php echo CHARSET; ?>
The built-in variable "CHARSET" is very strange and I've never seen this in PHP programming before. I've tried extensive search for "CHARSET php apache encoding charset utf-8 osCommerce" with google, stackoverflow and of course in php.net but couldn't find any results about this PHP variable.
I need further information because it types out unexpected/wrong content which doesn't fit to file encoding, apache transmission encoding nor charset/locale on the system.
So I want to ask: Where does CHARSET get its content from? Do you know any documentation for it?
See: http://forums.oscommerce.com/topic/321294-where-to-define-charset/
Or read it here, as I've copied it from there... :P
Its in catalog/includes/languages/english.php. Change english.php to the language file you need. Look for:
// charset for web pages and emails
define('CHARSET', 'iso-8859-1');

Categories