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
Related
I am fetching data with mysql_fetch_assoc() the string is Arabic it's echoing like this ?????
I tried encoding but not worked.
$dbc = mysql_connect("localhost", "root", "", "tedmon") or die("Error 3: " . mysql_error());
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
$sqlForStudent = "SELECT `MONITORING_PROVINCE`,count(*) FROM `student_interview` group by `MONITORING_PROVINCE`";
$sqlForadmin = "SELECT `pro`,count(*) FROM `ttc_admin_interview` group by `pro`";
$sqlForteacherev = "SELECT `pro`,count(*) FROM `ttc_teachers_evaluation` group by `pro`";
$sqlForteacherint = "SELECT `pro`,count(*) FROM `ttc_teachers_interview` group by `pro`";
$studentset = mysql_query($sqlForStudent);
$adminset = mysql_query($sqlForadmin);
$teacherevset = mysql_query($sqlForteacherev);
$teacherintset = mysql_query($sqlForteacherint);
while($teacherevres = mysql_fetch_array($teacherevset)){
echo $teacherevres['pro'];
}
Change from
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
To
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
Make sure you have added charset in head your page.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Note mysql extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
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 need to use bind_param to get away with data injection. When I use bind_param, special characters such as € or ب are being saved in mysql as صذق.
1-I am sure mysql table is set up correctly.
2-I have changed the word processor's encoding to UTF8.
3-I have included many utf8 character sets all over the place.
Any thoughts how to fix this? or maybe I should start using other methods such as mysqli_real_escape_string?
($_POST["post_word"] is generated in a separate page)
$connect = mysqli_connect("r"," r","r","r"); //not real data here
$connect->set_charset('utf8');
$connect->query("SET NAMES utf8");
$querye= mysql_query("SET NAMES utf8");
mysqli_query($connect,"INSERT INTO wordtable ( wdate) VALUES (CURRENT_TIMESTAMP())");
$sqlz = "SELECT wid FROM wordtable ORDER BY wdate DESC LIMIT 1";
$resultz = mysql_query($sqlz);
$rowz = mysql_fetch_array($resultz);
$wid=$rowz['wid'];
$mysqli = new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
$connect->set_charset('utf8');
$unsafe_variable = $_POST["post_word"];
$stmt = $mysqli->prepare("UPDATE wordtable SET word=(?)
WHERE wid='$wid' ");
$stmt->bind_param("s", $unsafe_variable);
$stmt->execute();
$stmt->close();
$mysqli->close();
Would it be perhaps
$querye= mysql_query("SET NAMES utf8");
Your querying with MySQL here
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 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.