change mysql table character set - php

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.

Related

How to get output from mysql in right encoding

how can i get MYSQL output in right encoding? I have this escaped string in MYSQL DB: Wannahëal, i need Wannahëal.
Thank you!
// Connect
$link = mysql_connect('xxx', 'xxx', 'xxx') or die("Can't connect to MYSQL DB");
mysql_set_charset("UTF8", $link);
//Choose a DB
mysql_select_db('xxx') or die("Can't select MYSQL DB");
$query = "SELECT * FROM mr_characters";
$result = mysql_query($query) or die('Query failed!');
header('Content-type: text/html; charset=utf-8');
while($row = mysql_fetch_object($result))
{
echo $row->title . "<br>";
}
You can use mysql_set_charset to set your client character set.
You need to set the charset in your charset, I think in your case utf8.
Either you can make this in the creating of your db:
CREATE DATABASE myDB DEFAULT CHARACTER SET utf8;
[...]
Or in the tables:
[...]
CREATE TABLE myTable DEFAULT CHARACTER SET utf8;
(
[...]
EDIT: If you don't set the charset before you make the data input into your MySQL server, your MySQL server isn't able to read the data correctly and saves them incorrectly. So not your code is incorrect nor your server had an any failure, quite honestly your server saved your data incorrect.

MySQL insert does not work

I want to insert data in mysql database.when i am trying to insert my query using PHP MyAdmin then work but if i am tring to insert from my php site from submission the not work some text not insert.
my text
http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:
my form submission insert only this text
http://www.lexisnexis.com/hottopics/gacode/
my query
$test='http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:';
$conn = mysql_connect('localhost', 'test', 'test') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query('SET NAMES utf8');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection ='utf8_unicode_ci'");
$queryc = "INSERT INTO `table` (data17)values ('".addslashes($test)."')";
mysql_query($queryc) or die(mysql_error());
for the database connection, it should be:
$conn = mysqli_connect('localhost', 'test', 'test') or die(mysqli_error($conn));
for the query, it should be:
mysqli_query($conn, $queryc) or die(mysqli_error($conn);
remember, the mysqli_query's arguments are database connection, query
also, it would be better if the query ($queryc) was:
$queryc = "INSERT INTO `table` (data17)values mysqli_real_escape_string($test))";

MySQL - PHP Select - doesn't show diacritics

I'm trying to SELECT elements from a database. This database uses the UTF-8 encoding.
For example, in one column named title, I have a row with this text: Crăciunul. It appears fine in phpMyAdmin as Crăciunul.
Now, I'm trying to connect to database in PHP and SELECT this database element. The problem is that it appears as Cr?ciunul on my website, which has the UTF-8 encoding setted, too.
This problem appears only if I get elements form database. I have made a string in PHP = Crăciunul, and it is shown fine on the website.
Thanks a lot!
EDIT 1:
$con = mysql_connect("HOST","USER","PASS");
mysql_select_db("DB", $con);
if (mysql_errno($con) !== 0)
{
die("Failed to connect to MySQL: " . mysql_error());
}
if (!mysql_select_db("DB"))
{
die('Could not select database: ' . mysql_error());
}
$query = mysql_query("SELECT * FROM table LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo $row['title'];
}
Set the character encoding of the DB connection.
$con = mysql_connect("HOST","USER","PASS");
mysql_set_charset('utf8', $con);
Also, please don't use this deprecated mysql API for new development.
try this ...
place this code before executing query ...
mysqli_set_charset ($dbconnection, "utf8");
try this
mysql_query('SET NAMES UTF8');
and
mysqli_set_charset($conn,"UTF8");

json_encode, utf8 and Greek characters

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.

Saving Japanese characters with 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

Categories