MySQL UTF8 Characters load - php

OMG I AM SOO SORRY I HAVE WRONG CODE TYPE :D HERE IS AN EDIT
OK I somehow figured out how to import UTF8 characters in MYSQL but when I load them they are not UTF8. And YES I have
<meta charset="UTF-8"/>
Look At this http://prntscr.com/b13xel . First post is OK but its not UTF8 it is from normal chars latin i think. But 2ND post isnt working :/ I have stored with UTF8 Charset in mysql http://prntscr.com/b13y2n (2ND is Test and Test2 if you dont get it xD) . I think I spelled it wrong xD but NVM I think you will understand me.
This is the code:
<div class = "tablatekst">
<?php
$novostid = 1;
while($novostid < 500)
{
$sqlnovosti = mysql_query("SELECT * FROM novosti WHERE ID = $novostid");
if(mysql_num_rows($sqlnovosti) > 0)
{
while($red = mysql_fetch_assoc($sqlnovosti))
{
$nnaslov = $red['Naslov'];
$ntekst = $red['Tekst'];
}
echo "<h2> $nnaslov </h2>";
echo $ntekst;
echo "<br><hr><br>";
}
$novostid = $novostid + 1;
}
?>
</div>

Put this before your while:
mysql_query('SET NAMES utf8');
However, I strongly recommend you to migrate to mysqli

What are the content-type headers set to in your PHP script?
https://stackoverflow.com/a/4279294/6275228
Use header to modify the HTTP header:
header('Content-Type: text/html; charset=utf-8');
Note to call this function before any output has been sent to the
client. Otherwise the header has been sent too and you obviously can’t
change it any more. You can check that with
headers_sent. See the manual page of
header for more information.

Related

Data is UTF-8, Ajax is returning some characters incorrectly

I have spent a lot of time searching the web and seeing a lot of similar answers, but cannot find anything that works for my situation.
I spent some time converting my MySQL database to UTF-8 from latin1 (the default). I emptied the tables (truncate), and re-imported the data from a text file. I have set the headers for my pages to use the meta tag to UTF-8:
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
And that's being used everywhere. When I read data via PHP and output it into forms, and such, the data appears correctly, text like: Königstadt appear as one might hope. Saving them (update set ...) they seem to be okay, because when I re-read them from the data the display in the form is correct. (The display in PHP Admin shows "Königstadt", which is weird, but when I read the data it seems to be correct ... -- I am hoping this is some oddity of PHPAdmin)
Where everything seems to be falling down on me is my Ajax code, when I retrieve data via PHP using Ajax. The following is a relatively simple routine that calls a PHP program to generate the option tags for an SELECT:
function get_branches()
{
// numeric values that need to be passed to the
// routine (so we show the correct item selected)
var region = document.getElementById("search_region").value;
// the code in load_branches needs this, but ...
var branch = document.getElementById("search_branch").value;
$.ajax
({
type: "POST",
url: "<?php echo $Roster_html_RootPath; ?>lookups/load_branches.php",
data: {
'region' : region,
'local_branch' : branch
},
//cache: false,
success: function(data)
{
// load contents of DIV tag with id of branchoptions:
$("#branch_options").html(data);
} // end success
}); // end ajax call
}; // end function get_branches()
}); // end document.ready ...
Most of the records returned are fine. However the one shown above (Königstadt) looks like: Königstadt in the HTML Select that is returned.
I have been trying to find a solution, such as setting contentType for Ajax, here are things I have tried:
contentType: "application/x-www-form-urlencoded;charset=utf-8",
This one doesn't seem to make any difference at all. Nothing changes.
contentType: "application/text; charset=utf-8",
(or application/json)
This kills the values being passed to the PHP file -- the data array doesn't seem to get there, because I get errors from PHP:
Notice: Undefined index: region in C:\xampp\htdocs\Heralds\Roster\lookups\load_branches.php on line 32
Notice: Undefined index: local_branch in C:\xampp\htdocs\Heralds\Roster\lookups\load_branches.php on line 33
I am at a complete loss how to return the values correctly. I need versions for both text or html (where I return an html table or the option tags as here), but I also need to use the json array for some of my code to return the values correctly. None of them seem to work properly with UTF-8 encoded data. I have been working on this for some time now, and am very frustrated. The explanations I am seeing are not working or in some cases not making sense ...
PHP lookups/load_branches.php
<?php
// if session has not started:
session_start();
// load some basic configuration, including relative paths
// and variables needed ...
include_once( "../includes/configuration.php" );
// data connection
include_once( $Roster_RootPath . "includes/connect.php");
// values from Ajax code:
$region = $_POST["region"];
$local_branch = $_POST["local_branch"];
// open the roster_branches table and get list
if( $region > 0 ) // check only needed for find_by_branch.php
{
$branch_statement = "select * from roster_branches where region=" . $region . " order by local";
}
else
{
$branch_statement = "select * from roster_branches order by local";
}
// first, get the data from the table:
$branch_result = mysqli_query( $connect, $branch_statement );
if( !$branch_result )
{
$out = "";
$out .= "<div class='alert alert-danger'>";
$out .= "<p><b>Error in SQL statement ...</b><br />";
$errornum = mysqli_errno( $connect );
$out .= "MySQL Error Number: " . $errornum . "<br />";
$out .= "MySQL Error: " . mysqli_error( $connect ) . "<br />";
$out .= "SQL Statement: " . $branch_statement . "</p>";
$out .= "</div>";
echo $out;
die;
}
else
{
$out = "";
// create select:
$out = "<select class='form-control' id='local_branch' name='local_branch'>\n";
// need the blank option:
$out .= " <option value=0 selected></option>\n";
while( $branch_row = mysqli_fetch_array( $branch_result ) )
{
$id = $branch_row["rb_id"];
$local = $branch_row["local"];
$selected = "";
if( $local_branch == $id )
{
$selected = " selected";
}
$out .= "<option value=" . $id . $selected . ">" . $local . "</option> \n";
}
$out .= "</select>\n";
echo $out;
} // we have something
?>
my MySQL database to UTF-8 from latin1 (the default).
1) I used "utf8_unicode_ci". 2) I have no idea what "multibyte safe functions" you're talking about. I use mysqli_real_escape_string() when reading data from $_POST, and the usual mysqli_query() and so on functions
This is the cause of your issue.
There are 3 main places to work to correct this:
1)
You need to enable true UTF-8 (4-byte) in MySQL so that data stored in your SQL is stored as the correct UTF-8 characters. by universally using collations and character sets with the utf8mb4_ prefix.
2)
To ensure data from your application/PHP is saved correctly you then need to ensure that the data is passed to MySQL as UTF-8 4-byte characters by setting the connection character set to full (4-byte) UTF-8 in your PHP:
$mysqliObject->set_charset('utf8mb4'); // object oriented style
mysqli_set_charset($connect, 'utf8mb4'); // procedural code style
3)
Finally; you need to ensure that any processing PHP does with the resulting data is multi-byte aware; by using the mbstring set of functions.
Most notably:
mb_http_output() — Detect and convert HTTP output character encoding
mb_internal_encoding() — Set PHP's internal character encoding
So your top of each PHP page should look like this before any browser output)
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
Then if you do anything with str_<whatever> functions (and some others) you know it won't break you strings before they're output to the browser (ajx in this case).
If you can't use mbstring functions for any reason then check out here [how to install them](http://www.knowledgebase-script.com/kb/article/how-to-enable-mbstring-in-php-46.html
).
Please also review this excellent Q&A has helped you at all?
Further reading about solving the MySQL side of things can be found here.
ö is "Mojibake" for ö. See "Mojibake" in Trouble with UTF-8 characters; what I see is not what I stored .
But the real problem probably happen when you converted from latin1.
The tables needed to be converted via ALTER TABLE .. CONVERT TO CHARACTER SET utf8mb4. Any other technique is likely to make a mess.
Any connection to MySQL needs to be specify utf8mb4.

How to handle Japanese characters in HTML , PHP & MySQL

I have written the following code in order to read content from an Excel file that contains Japanese characters and display on the web page:
<!DOCTYPE html>
<?php
//header("Content-Type: text/plain; charset=UTF-8"); // output as text file
header("Content-Type: text/html; charset=UTF-8");
if(isset($_POST['upload'])){
unset($_POST['upload']);
$file_name = basename($_FILES['csv_file']['name']);
$name = pathinfo($file_name, PATHINFO_FILENAME );
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$csvFile = fopen($_FILES['csv_file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
$flag = true; // flag set false when query fails for one or more records
while($line = fgetcsv($csvFile)){
if(count($line)>0){
$data = utf8_decode($line[0]);
echo "$data <br>";
}
}
if($flag)
echo "<h1 style='color:limegreen'> All records imported successfully ! </h1>";
else
echo " Error while fetching one or more records";
fclose($csvFile);
}
?>
<form method="post" action="importExcel.php" enctype="multipart/form-data">
<input type="file" name="csv_file" id="csv_file" accept=".xlsx" >
<input type="submit" name="upload" id="upload" >
</form>
This is the excel sheet with the Japanese characters:
My question is :
How do i make those Japanese characters to display properly on web browser? I tried using the utf_decode() function. It did not help.
Also if i want to store these Japanese alphabets in MySQL database, what changes will i be required to make?
Currently the browser displays the Japanese characters as question marks and some garbage values after using utf_decode() function. When i remove it, it displays junk values on screen.
Edit:
Here is the sample data from the excel file:
アイリッシュ・セッター アイリッシュ・ウォーター・スパニエル アイリッシュ・ウルフハウンド
Do not use any encoders/decoders, that will only make things worse.
Do have CHARACTER SET utf8 (or utf8mb4)
Do have the header or meta, as already discussed.
Do set the connection between php and mysql to be UTF-8. (Let's see that part of the code to check.)
Do specify UTF-8 in <form method="post" ... accept-charset="UTF-8">
Please show us what went wrong. There are at least 5 variants of "not working", as can be seen here: Trouble with utf8 characters; what I see is not what I stored
If you can get the HEX, note that utf8 encoding for アイリッ is E382A2 E382A4 E383AA E38383. Other Katakana characters are also E3xxyy. (Kanji and Hiragana are also 3 bytes: Ewxxyy.)
You don't need utf8_decode, adding header("Content-Type: text/html; charset=UTF-8"); will work for you. like->
NOTE: utf8_decode This function converts the string data from the UTF-8 encoding to ISO-8859-1. but you need utf-8 in your case.
NOTE: using fgetcsv may not give the desired data when reading excel file . convert it in csv or use some excel reader library.
header("Content-Type: text/html; charset=UTF-8");
$str="七起千去";
echo "$data <br>";
NOTE: if you are not using header add <meta charset="utf-8"> to your page this should also work.

PHP image does not show when using <img>

I have found a lot of posts that describe the same problem, but everything I tried failed.
For a profile page I am making people can upload 4 pictures. When no picture is available I want to show a default picture. My complete code is like this now:
$id = $_GET['ID'];
$link = mysql_connect("xxx", "xxx", "xxx");
mysql_select_db("xxx");
$sql = "SELECT * FROM profielen WHERE ID = $id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
if ($row['Foto4'] == NULL){
//echo '<img src="/img/noImage.jpg" />';
echo $row['Foto5'];
}
else {
echo $row['Foto4'];
}
In this form the code works, so I know that the if-statement is correct.
When I try to uncomment the commented line, it shows a broken link as the image.
I have tried with double quotes and then escape the double quotes within the img tag. I have also tried to call it as a variable and without a / before the img-path. And also just tried to say echo "No image", but it seems that if I do anything else than echo $row[x] it just does not work. When I try to display the image in HTML it works fine, so the name of the file is correct.
I am running out of ideas, so maybe someone can help?
The problem with your code is that you're sending HTML to embed an image back, when really, you should be sending image data back. You can solve this by redirecting the user to the "noImage.jpg", in this way the user will get an image back from the request and all will be fine :)
Code for this could look like this:
if ($row['Foto4'] == NULL){
header('Location: /image/noImage.jpg');
} else {
echo $row['Foto4'];
}
If you use the content type image/jpeg, the browser expects the raw binary data of an image. Instead, you give it HTML-code.
You can instead use the Location-header, which tells the browser to open another address.
header("Location: /img/noImage.jpg");
Remember that you cannot output any other data or write any other headers if you use this method.

json_encode adding special characters

Now without having to read boring code let me just summarise what is going on.
An XHR request goes out via jQuery post function when the document loads. The post sends a POST header to a PHP file that calls another php file to process some strings and return it. Finally an echoed json_encode function returns the string to the jQuery post function which the jQuery then manipulates into the DOM.
The result that is returned by JavaScript's post function is this:
<option value="\"auto2\"">auto2<\/option></option>
See all those escape characters? Well I tried just echoing the result on the PHP side instead of echoing with json_encode function and it returns:
<option value="auto">auto</option>
Without any other special characters. This is what is desired. My question is, is this okay to use? And why does json_encode add special characters? Is there a way to use json_encode without those other characters being returned?
EDIT
JS
$(document).ready(function() {
var PROCESSOR_FILE = "http://address/include/processor.php";
$.post(PROCESSOR_FILE, {"task": "verticalSelect"}, function(data) {
$("#verticalSelect").html(data);
});
});
PHP
$IMEXporterpath = dirname(__FILE__);
$IMEXporterFile = $IMEXporterpath . "/IMEXporter.class.php";
if (isset($_REQUEST["task"]))
$task = $_REQUEST["task"];
else
$task = "";
require_once $IMEXporterFile;
$IMEXp = new IMEXp();
$result = $IMEXp->returnHtml($task);
echo json_encode($result);
MORE PHP
public function returnHtml($element) {
return $this->returnHtml_HTML($element);
}
private function returnHtml_HTML($element) {
if($element == "verticalSelect") {
$dbTables = $this->get_Tables();
$nbTable = count($dbTables);
for ($a = 0; $a < $nbTable; $a++) {
$ZHTML .= '<option value="'.$dbTables[$a].'">'.$dbTables[$a].'</option>';
}
}
return $ZHTML;
}
On PHP 5.3.13:
header("Content-Type: text/plain"); // to make sure browser displays raw html
$str = '<option value="auto">auto</option>';
echo json_encode($str);
echo "\n";
echo $str;
Outputs
"<option value=\"auto\">auto<\/option>"
<option value="auto">auto</option>
No converted special chars here.
$.post('test.php').done(function(text){
return console.log(text);
});
Prints the same to the console.
Without AJAX does json_encode($str) display " etc. ?
Without seeing some code, it's hard to know what's happening, and maybe I didn't understand your question. In my experience, it's best to put everything in UTF-8. I can't remember exactly, but UTF-8 is the default for AJAX in some situations. Sorry for my vagueness, but there doesn't seem to be a good workaround if you're not using UTF-8.
That means that you should change all the tables in your database to UTF-8, save all your files in UTF-8 format (I use TextPad to change the format), and specify all your HTML pages to UTF-8.
I had to go through that unpleasant experience, because I develop sites in portuguese with a lot of accented characters. I used to try to use ISO-8859-1, but after working more with AJAX, I found that UTF-8 is really the only viable option.

PHP setcookie warning

I have a problem with 'setcookie' in PHP and I can't solve it.
so I receive this error "Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\vote.php:14) in C:\Program Files\VertrigoServ\www\vote.php on line 86"
and here is the file..
line 86 is setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
is there any other way to do this ??
<html>
<head>
<title>Ranking</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#EEF0FF">
<div align="center">
<br/>
<div align="center"><div id="header"></div></div>
<br/>
<table width="800" border="0" align="center" cellpadding="5" cellspacing="0" class="mid-table">
<tr><td height="5">
<center>
</tr>
</table>
</center>
</td></tr>
<tr><td height="5"></td></tr>
</table>
<br/>
<?php
include "conf.php";
$id = $_GET['id'];
if (!isset($_POST['submitted']))
{
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
</div></td></tr>
<tr><td align="center" valign="top"><img src="images/ads/top_banner.png"></td></tr>
</table>
</form>
<?php
}
else
{
echo '<font color="red">You must select a valid server to vote for it!</font>';
}
}
else
{
$kod=$_POST['kod'];
if($kod!=$_COOKIE[imgcodepage])
{
echo "The code does not match";
}
else
{
$id = mysql_real_escape_string($_POST['id']);
$query = "SELECT SQL_CACHE id, votes FROM s_servers WHERE id = $id";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$votes = $row['votes'];
$id = $row['id'];
$cookie_name = 'vote_'.$id;
$ip = $_SERVER['REMOTE_ADDR'];
$ltime = mysql_fetch_assoc(mysql_query("SELECT SQL_CACHE `time` FROM `s_votes` WHERE `sid`='$id' AND `ip`='$ip'"));
$ltime = $ltime['time'] + 86400;
$time = time();
if (isset($_COOKIE['vote_'.$id]) OR $ltime > $time)
{
echo 'You have already voted in last 24 hours! Your vote is not recorded.';
}
else
{
$votes++;
$query = "UPDATE s_servers SET votes = $votes WHERE id = $id";
$time = time();
$query2 = mysql_query("INSERT INTO `s_votes` (`ip`, `time`, `sid`) VALUES ('$ip', '$time', '$id')");
$result = mysql_query($query) OR die(mysql_error());
setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
}
}
}
?>
<p>[Click here if you don't want to vote]</p><br/>
<p>Ranking.net © 2010-2011<br> </p>
</div>
</body>
</html>
Thanks a lot!
You cannot have any output before header() and setcookie() calls.
https://stackoverflow.com/search?q=+headers+already+sent+by
https://stackoverflow.com/tags/php/info
Any output includes any <html> before the openeing <?php marker, or any print or echoing of content. Another culprit is the UTF-8 BOM http://en.wikipedia.org/wiki/Byte_Order_Mark - which most text editors do not show visibly, but confuses PHP when at the beginning of files.
Setting a cookie requires sending a header to the client, and you can't send headers if the output has already started.
You have to put the PHP code before the HTML markup so that you can call setcookie before any output is sent and you also separate PHP code from presentation which you should do anyway.
You should put the cookie code at the top of the page. A better layout would be something like this:
<?php
//include config
//check posted data (included settings cookies)
//set needed variables
?>
<html>
.....
You could also separated the php code and html. This is generally what i do. My uses generally involve a view class or (in the past) smarty. but a quick example would be to add this code at the bottom of the above php code and get rid of the html:
<?php
if(empty($tpl)) {
$tpl = 'index';
}
if(file_exists("template/{$tpl}.tpl.php")) {
include("template/{$tpl}.tpl.php");
}
else {
header('Location: /');
}
?>
YOu would need to create a directory called 'templates' and add the html code to files that end in the .tpl.php extension.
Really they are just php pages, but the .tpl. part help you remember that its just mark up.
Make them php pages (not html) so you can output variables
Then in your varios parts of your code above you would set $tpl to be the template you want to load.
This is a just a base bit of code, but it should give you a general idea on how to separate this data. THe main idea is that all html and text will be outputted "after" all programming code has been done.
What you need to do is to create a nice buffer to hold all the headers in until you are down processing. ob_start does the job for you and here is a reference to it. http://php.net/manual/en/function.ob-start.php
When you are finished loading all the different headers use ob_get_contents like this
$text = ob_get_contents();
echo($text);
ob_end_clean();
I hope this helps.
You can not use PHP to set a cookie after any thing has been outputted, and the html before the first php tag does count as output. to keep to a purely php method you'd have to move the whole part about determining what to put int he cookie and setting it up to the very top.
or what I do in situations where that would require to much extra work to do it that way is to have php echo out the JavaScript code to set a cookie. now if you make or get a nice JS cookie setting function and either embed it or link it into the page. then all you have to do is have php echo the function call with the proper data in it at that point. then when the page loads while php still will not set the cookie, but when the browser when it runs the js code it will. and so you get what you want the cookie is set. and you did not have to move the stuff up to the top.

Categories