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.
Related
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");
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.
Thanks for taking a look:
Here is the php I'm using to insert the data into the table
<?php
session_start();
//sets a variable from a session value
if (isset($_SESSION['sv_01'])) {$sv_01=$_SESSION['sv_01'];} else {$sv_01="";}
//to test that the variable has been set and is not empty
echo $sv_01;
//define database log in stuff
$username="username123";
$password="password123";
$database="database01";
$table="my_table";
$dbaddress="123.123.123.123";
//connect to dbserver
$con=mysql_connect($dbaddress,$username,$password);
if (!$con)
{
die('Could not connect:' .mysql_error());
}
//select the db
mysql_select_db($database) or die( "Unable to select database");
//insert data from variables
mysql_query("INSERT INTO $table
(
$sv_01
)
VALUES
(
'$sv_01'
)");
mysql_close($con);
?>
I run this, and then go to check out the contents of the DB. Using MySQL workbench I open the connection and the database and table in question, select all rows and there is no data contained in the table.
MySQL info:
Collation: latin1 - default
collation Engine: MyISAM
datatype: sv_01 VARCHAR (255)
default: NULL
Any ideas what I am doing incorrectly?
I believe that the name of the field is sv_01 not $sv_01
I would try:
$query = "INSERT INTO $table (sv_01) VALUES ('$sv_01')";
Update (dedicated to tadman):
A small piece of advice: DO NOT use mysql_query
Use localhost insted af your IP (if possible), and make your connection easy to read:
$con=mysql_connect($dbaddress,$username,$password) OR DIE mysql_error();
AND you also have to give you mysql_query a variable:
$mysql = mysql_query("INSERT INTO $table ($sv_01) VALUES ('".$sv_01."');");
:)
Unable to perform the query statement Incorrect string value: \x96\xBC\x91O\x82\xCD... for column 'member' at row 1.
Here is the code.
$member=stripslashes($_POST["membername"]);
$link=mysql_connect(localhost,$username, $password) or die("unable to connect to database ".mysql_error());
mysql_set_charset('utf8', $link);
mysql_selectdb($database) or die("unable to select db ".mysql_error());
$query="SELECT*FROM members";
$result=mysql_query($query);
$rowNum=mysql_num_rows($result)+1;
$query="INSERT INTO members VALUES ('$rowNum','$member','$memberemail','$memberpass','$memberRegDate','$memberRegDate','$memberIP')";
mysql_query($query) or die("Unable to perform the query statement. ".mysql_error());
EDIT; I already change my DB as this
ALTER DATABASE users CHARACTER SET UTF8 COLLATE UTF8_UNICODE_CI;
and
ALTER TABLE members CHANGE name name varchar(100) character set utf8;
That's not UTF-8. You should be looking at the HTTP headers in the request to see what charset is being used, and convert appropriately.
>>> print '\x96\xBC\x91O\x82\xCD...'.decode('cp949')
뼹멟궼...
See the code below, there are two databases connections.
First it get the data from first connection and then insert into second database connection but it will not insert - I can an error saying Unknown column 'fullname' in 'field list'
When I tried SQL query manually in phpMyAdmin and it work fine...
$db_new = mysql_connect('localhost', 'root', 'password');
if (!mysql_select_db("menu_new", $db_new)) {
die("Cant connect menu_new DATABASE");
}
$db_old = mysql_connect('localhost', 'root', 'password');
if (!mysql_select_db("old_menu", $db_old)) {
die("Cant connect old_menu DATABASE");
}
$SQL_old = "SELECT * FROM old_table";
$q = mysql_query($SQL_old, $db_old);
while ($row = mysql_fetch_assoc($q)) {
$name = $row['name'];
$SQL = "INSERT INTO tbl_name (fullname) values ('$name')";
//Problem Here - It wont insert into second database
mysql_query($SQL, $db_new) or die(mysql_error($db_new));
}
Nothing is strange in this behavior. Just add the $link parameter on your mysql_connect calls, and set it to true. By default it is False and it means that reusing this function with the same parameters, which is what you're doing as the db name is not on your mysql-connect, will reuse existing connexion with same parameters. So you have two variables but only one connexion.
It means you're simply moving the db used in this connexion. Prefixing with the db name fixed the problem as MySQL allow inter-base manipulations from the same connexion if it's on the same db server.
Thanks #Konerak for suggestion and that does work!
To Insert/Select data from Database connection, you will have to include database name before the table name.
For Example
From:
mysql_query("INSERT into tbl_name (fullname) values ('1')", $db_new) or die(mysql_error($db_new));
To:
mysql_query("INSERT into menu_new.tbl_name (fullname) values ('1')", $db_new) or die(mysql_error($db_new));
That is really odd though.