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
Related
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.
I have encountered a problem, when i try to execute a sql statement I have stored in a file. The problem is that special characters like æøå becomes gibberish when they are imported to the database.
I use the code below:
$dsn = 'mysql:host='.DBHOST.';dbname='.DBBASE;
$db = new PDO($dsn, DBUSER, DBPASS);
//$db->exec("set names 'utf8'");
$sql = "TRUNCATE TABLE wtm_meta_language";
$db->exec($sql);
$sql = file_get_contents('language.sql');
$db->exec($sql);
The language.sql file is encoded in UTF-8 and so is the database.
I have tried to force the database connection to use utf-8 by adding "set names 'utf8'" to my code (row 3, that is now a comment), but when I add this row nothing gets imported at all.
I hope someone has an idea to how to solve this.
Your results are not getting retrieved because you did not add charset=utf8 .
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBBASE.';charset=utf8', 'username', 'password');
and then it should allow you to get the required result.
Could be that PHP does not detect that the file is UTF-8 since, according to your comment, the BOM is missing.
I found this little function here which should force it to read UTF-8.
function file_get_contents_utf8($fn) {
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
$result = #file_get_contents($fn,false,$context);
return $result;
}
Also, if I were you I'd use mysql_set_charset to set the character set on the mysql connection. Also I'd use SHOW VARIABLES LIKE 'character_set%'; so see what the current connection character set is.
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.
Hello i have problem with encoding in my script.
My connect function looks like:
function connect()
{
$conn = mysql_connect('192.168.1.127', 'mason_frik', 'difficultpassword');
if (!$conn)
{
die('Nie można się połaczyć!');
}
mysql_query("SET NAMES 'utf8'; COLLATE='utf8_polish_ci';");
mysql_query("SET character_set_client = 'utf8'");
mysql_query("SET character_set_results = 'utf8'");
mysql_query("SET character_set_connection = 'utf8'");
mysql_select_db('mason_konkursy');
}
In my database i'm using utf8_polish_ci everywhere.
In my script i'm getting something from other page and i need to search it in my db like this:
//this function is parsing other page and get innertext of SPAN.
$question = GetSpanData($FirstQuestion, "dnn_ctr1975_ViewContestsContestNew_dc_question_lblQuestion");
$wyn = mysql_query('SELECT * FROM questions WHERE question="'.$question.'"');
$wynik = mysql_fetch_array($wyn, MYSQL_ASSOC);
Result is bool(false).
When i copy query to my phpmyadmin and paste to sql it is working, but from my script it didn't.
Can You help me?
This line is wrong:
mysql_query("SET NAMES 'utf8'; COLLATE='utf8_polish_ci';");
The correct syntax is:
SET NAMES 'charset_name' COLLATE 'collation_name'
In your case, the code would the be:
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_polish_ci';");
And read the first comment posted by eggyal: mysql_* functions should not be used anymore, they are deprecated and will be removed from PHP in a future version.
You may be missing a call to mysql_real_escape_string in this line of code (it is not possible for me to be sure, it depends what the function GetSpanData exactly does):
mysql_query('SELECT * FROM questions WHERE question="'.$question.'"');
to escape the data properly, you must use the mysql_real_escape_string function:
mysql_query('SELECT * FROM questions WHERE question="'.mysql_real_escape_string($question).'"');
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.