I use the following code to store and update data in my db.
<?php
header("Content-Type: text/html;charset=UTF-8", true);
mysql_connect("localhost", "test", "test") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// mysql_set_charset('UTF8');
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
mysql_query("UPDATE `test`.`english` SET age = '".$age."', city = '".mysql_real_escape_string($_REQUEST['city'])."', aboutMe = '".$_REQUEST['about']."' WHERE `english`.`username` = '".$_REQUEST['username']."' LIMIT 1 ;")
or die(mysql_error());
The code is working well. But if the string, e.g. "city" contains a ä,ö,ü it is stored as ?
So for example the word "Bär" goes to "B?r".
I have already added these utf8 things but it does not help.
The server is only supporting php4.x
Edit: The DB is using latin1_general_ci as collation.
Use utf8 as collation, maybe utf8_bin or utf8_unicode_ci
(The ci means it's case insensitive so comparsions like "John" = "JOHN" return true). You can do this by running the following query in MySQL:
alter table `test`.`english` convert to character set utf8 collate utf8_unicode_ci;
change the table collation to utf8_bin.
To change the default character set and collation of a table including those of existing columns (note the convert to clause):
ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin
in utf8_general_ci
ä = a
ö = o
ü = u
but in utf8_bin
ä != a
ö != o
ü != u
Keep ut8_unicode_ci and add this to your php code:
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_query("set names 'utf8'",$connection);
I am using this everywhere (Cyrillic, Chinese etc ) and it is working for every query (insert or update).
<?php
header("Content-Type: text/html;charset=UTF-8", true);
$connection = mysql_connect("localhost", "test", "test") or die(mysql_error());
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_query("set names 'utf8'",$connection);
mysql_select_db("test") or die(mysql_error());
mysql_query("UPDATE `test`.`english` SET age = '".$age."', city = '".mysql_real_escape_string($_REQUEST['city'])."', aboutMe = '".$_REQUEST['about']."' WHERE `english`.`username` = '".$_REQUEST['username']."' LIMIT 1 ;")
or die(mysql_error());
I got it working. I have no explanation for this behavior.
I have changed in the php file from utf8 to latin1:
mysql_query("SET NAMES 'latin1'");
mysql_query("SET CHARACTER SET latin1");
mysql_query("SET COLLATION_CONNECTION = 'latin1_general_ci'");
In the MySQL DB the collation is utf8_unicode_ci for the table and column.
It is working now for all characters beside the € symbol.
Related
first of all , I connected two database -mssql & mysql- .
global $link,$link_voip;
$link = mssql_connect('A', 'B', 'C');
mssql_select_db('D', $link);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
$link_voip = mysql_connect('E','F','G') or die("err");
mysql_query("SET NAMES 'utf8'", $link_voip);
mysql_query("SET CHARACTER SET 'utf8'", $link_voip);
mysql_query("SET character_set_connection = 'utf8'", $link_voip);
mysql_select_db('voip', $link_voip);
I want to update a row on mysql database with mssql's information:
I get information correctly , but mysql database cannot insert utf-8 (persian language).
I'm working with phpmyadmin ,I used to try:1- change collation to utf_general_ci 2- add
mysql_query("SET NAMES 'utf8'", $link_voip);
mysql_query("SET CHARACTER SET 'utf8'", $link_voip);
before query.
3-add N before variable that may be changed.
$q="UPDATE cc_card SET firstname=N'$fname' , lastname=N'$lname',phone='$phone',credit='$credit'
WHERE id='$id'";
but still it doesn't work.
1- in mssql :
1-1. check table type is nvarchar
1-2. convert to varbinary (CONVERT(varbinary(MAX),tblname)
1-3. use bin2hex 1-3. use chr(hexdec$varibale)
1-5. use iconv
2- in mysql:
use N before variable for update/insert in database
I am writing an android app and i want to query a db and get the results with json_encode.
So i have this db:
CREATE DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
I use utf8 because i want to store greek characters. All the tables have utf8_general_ci collation.
At last i have this php service that i use to query my db.
<?php
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "my_db";
$con = mysql_connect($host,$username,$password) or die ('Error connecting to mysql');
mysql_query("SET character_set_results=utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);
mysql_select_db($dbname,$con);
$query = "SELECT * from patients";
$result = mysql_query($query,$con);
while($e=mysql_fetch_assoc($result))
$output[]=$e;
print(json_encode($output));
mysql_close($con);
?>
The problem is that in the output i dont get greek characters. Instead i get this:
[{"patient_id":"2","email":"patienta#mail.com","password":"password2","firstname":"\u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3","lastname":"\u039c\u03a0\u0391\u0396\u0399\u03a9\u03a4\u0397\u03a3","birth":"1989-11-11","mobile":"6941212121","tameio":"\u039f\u0393\u0391"}]
That is right. All the chars not in ascii will be converted into UTF-8 format.
The data will be decoded correctly by json_decode.
I am trying to insert Utf-8 string in a cell with utf8_general_ci collation
My code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
print mb_detect_encoding($name);
$query = "INSERT INTO items SET name='$name', type='$type'";
print $query;
mysql_set_charset('utf8'); // also doesn't help
$result0 = mysql_query("SET CHARACTER SET utf8") or die(mysql_error()); // I've added this line, but it doesn't solve the issue
$result01 = mysql_query('SET NAMES utf8') or die("set character ".mysql_error()); // still nothing
$result = mysql_query($query) or die(mysql_error());
What I see in the html output:
UTF-8 INSERT INTO waypoints SET name='ČAS', type='ts'
What I see in the database, as name in the inserted row:
?AS
Now my string is utf-8, my table and cell is utf-8 .. why the wrong encoding?
Ok guys, thank you all for help, it looks like I've solved the issue with:
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
right after creating the connection
(especially #Bartdude for the article, which gave me a hint)
I have a table that data was inserted in it some time ago. data included by php and character set in it was utf-8 .
mysql_query("set names utf8");
now ,I need this table in another project which all data will show on php mysql default character set.
problem : my data is persian, when I set charachter set utf-8, every things is ok, but without character set data convert to the "?????" what should I do?!
I want to import all data form old table to new table on new character set !!
Try this might help you
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>
///
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
mysql_query("set names utf8");
$sql = "SELECT * FROM `region` ";
$res = mysql_query($sql);
while($h=(mysql_fetch_array($res)))
{
$row[] = $h;
}
mysql_close($db);
/// set another character set
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
foreach ($row as $item)
{
echo $sql = "insert into `region2` (id,name,myId) values (".$item['id'].",'".$item['name']."','".$item['myId']."') ";
mysql_query($sql);
}
#afsane, using mysqldump, you can dump the data from old_table and then import it into the new_table which has the new character encoding.
mysqldump -u user -p --no-create-info schema old_table > old_table.dump
mysql -u user -p schema new_table < old_table.dump
This will be much faster than doing a conversion via PHP.
I'm currently trying to save a japanese-character string with MySQL in PHP. The characters are saved as questionmarks. This is my query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$result = mysql_query("INSERT INTO battles (basic_words) VALUES ('".mysql_real_escape_string($basic_words)."'");
The string "$basic_words" definitely contains japanese characters but they are not saved. The coalition for the row "basic_words" is utf8_general_ci
php mysql query encoding problem suggests
$db_con= mysql_connect('localhost', 'user', 'password');
if( function_exists('mysql_set_charset') ){
mysql_set_charset('utf8', $db_con);
}else{
mysql_query("SET NAMES 'utf8'", $db_con);
}
Also Check http://php.net/manual/en/function.mysql-set-charset.php