insert ≠ (not equal to ) in mysql field - php

I run the following query in mysql
UPDATE `gamequestions` SET a2 = '≠' WHERE id = 564
It runs successfully but the '?' is inserted in a2 field in place of '≠'
The datatype of a2 is text and also tried with varchar
Any Help greatly appreciated.

you need to change Collation to UTF-8 to store special characters

insert ≠ (not equal to ) in mysql field
The goal in these conversions is always to decide on what charset/collation combination you want to use (UTF8 being the best choice in almost all scenarios) then to convert all tables/columns in your database to use that charset. At that point you can set DB_COLLATE and DB_CHARSET` to the desired charset and collation to match.
Note:
In most cases if a collation is not defined MySQL will assume the default collation for the CHARSET which is specified. For UTF8 the default is utf8_general_ci, which is usually the right choice.
Changing the default charset of the database
ALTER DATABASE MyDb CHARACTER SET utf8;
Changing the default charset of individual tables
ALTER TABLE MyTable CHARACTER SET utf8;
https://dev.mysql.com/doc/refman/5.1/en/charset-unicode-utf8.html

You can add that option in the /mysql/my.cnf. In the [mysqld] section add ’’character-set-server=UTF8"; in the [client] section add “default-character-set=UTF8”.
You can find more information in these links:
http://dev.mysql.com/doc/refman/5.1/en/charset-… http://dev.mysql.com/doc/refman/5.0/en/server-o…
If you need to conver existing data, you can execute:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

You need to check following things
use set names utf8 before you query/insert into the database
using Default CHARSET=utf8 when creating new tables

Related

How to store special charcters as UTF-8 format in database

I am trying to store "£" in database. I also marked this field as utf8_general_ci in collection.But it save as �. I also set UTF-8 in meta tag HTML file. What is missing here
-----------------EDIT-----------
I checked that,there is one editor(Rich Text)on that page.If I use this editor to save data then it generate � but if I use textbox then it works fine. I don't know what's wrong in this and how to debug this issue
$query = "insert into field_master set fk_t_id = '".$_POST['t_id']."', fk_g_id = '".$_POST['g_id']."', fk_f_id = '".$_POST['f_id_'.$inc_i.'_'.$inc_j]."'".$str.", field_value = '".$field_value."', field_required = '".$required."', fk_section_id = '".$_POST['section_id_'.$inc_i]."', section_order = '".$_POST['section_order_'.$inc_i]."', field_order = '".$_POST['temp_order_'.$inc_i.'_'.$inc_j]."'";
$smt = $class->dbh->prepare($query);
$smt->execute();
The examples shown here assume use of the utf8 character set and utf8_general_ci collation.
Specify character settings per database. To create a database such that its tables will use a given default character set and collation for data storage, use a CREATE DATABASE statement like this:
CREATE DATABASE mydb
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
Tables created in the database will use utf8 and utf8_general_ci by default for any character columns.
Applications that use the database should also configure their connection to the server each time they connect. This can be done by executing a SET NAMES 'utf8' statement after connecting. The statement can be used regardless of connection method: The mysql client, PHP scripts, and so forth.
In some cases, it may be possible to configure the connection to use the desired character set some other way. For example, for connections made using mysql, you can specify the --default-character-set=utf8 command-line option to achieve the same effect as SET NAMES 'utf8'.
If you change the default character set or collation for a database, stored routines that use the database defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly.
Specify character settings at server startup. To select a character set and collation at server startup, use the --character-set-server and --collation-server options. For example, to specify the options in an option file, include these lines:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.
I hope these information would be useful to you.
The question mark in a black diamond when you have really messed up utf8 usage. The simple answer is to mess it up further by saying (in the html) <meta charset=ISO-8859-1> (alias latin1).
The 'right' answer is to use utf8 throughout:
Characters in the client are encoded utf8.
Connection is utf8 -- via executing SET NAMES utf8 or some language-specific syntax when connecting. (See below)
CHARACTER SET utf8 on the column/table.
HTML: <meta charset=UTF-8>
Different things happen depending on which of those you violate.
If those notes are not enough to solve your problem, then do SELECT col, HEX(col) FROM ... to see what is really in the table. For £, you should see hex C2A3. If you get C382C2A3, you have the "double encoding" problem. If you see (not hex) £, you have the "Mojibake" problem. If none-of-the-above, well, that is what makes character set problems a challenge.
COLLATION (such as utf8_general_ci) is for sorting; it is not relevant in this discussion, only the CHARACTER SET (utf8 or utf8mb4) is.
mysqli interface: mysqli_set_charset('utf8');
PDO interface: $db = new PDO('dblib:host=host;dbname=db;charset=UTF8', $user, $pwd);

Laravel 4 arabic characters

I have a laravel 4.2 project and I have a utf8 fields in database, but the way the data is stored in this filed is like را characters.
In any php file (other than laravel) after selecting the data from database those characters rendered in correct way after using (SET NAMES 'utf8'). I want to do same in laravel. (even if not a database solution)
Here is what i have tried:
make sure all files are utf8
make sure that in config files charset and collation are utf8
use PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" in config files
I also tried to use Blade::setEchoFormat('e(utf8_encode(%s))'); but did not know how to use it in correct way.
Any help would be appreciated.
Do not use iconv. Do not use utf8_encode.
You have Mojibake.
Were you expecting را instead of را? Is this the HEX that is in the table: D8B1D8A7?
This is the classic case of
The bytes you have in the client are correctly encoded in utf8 (good).
You connected with SET NAMES latin1 (or set_charset('latin1') or ...), probably by default. (It should have been utf8.)
The column in the tables may or may not have been CHARACTER SET utf8, but it should have been that.
If you need to fix the data it takes a "2-step ALTER", something like
ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;
where the lengths are big enough and the other "..." have whatever else (NOT NULL, etc) was already on the column.
ok here is how i get it to work :
my problem was the opposite of what i thought .
all what i did is removing utf8 settings from database.php config file , and comment
line $connection->prepare($names)->execute(); in MysqlConnector.php file
and the right words shown
thanks for all and if any one know a better solution please share with me.
Set CHARACTER SET to utf8 and COLLATE to utf8_general_ci of your database, table, and column.
ALTER DATABASE db_name CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE table_name MODIFY column_name column_name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci;

PHP Special Character Parse Error

I am parsing a XML and putting it into MySQL. Now MySQL is showing following error for some specific records inserting:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xEAm-Kh\xEA...'
When i check the data inside XML, it is showing like iêm-Khê
So the Special Character ê is transforming into xEA
What is it?
How can i get it solved to maintain & put its original Character into Database?
FYI, here is some info.:
My whole MySQL DB itself is using latin1_swedish_ci
But that TABLE & COLUMN are using utf8_general_ci
** When i manually Copy/Paste that iêm-Khê values into the MySQL Record directly, it is saved. (I mean, MySQL is accepting that Special Characters.) So i think the issue is at the Coding side.
After some additional research I ran across two possible approaches to a solution:
- the first option is to use php's utf8_encode() function.
- the second option uses some code with the PDO using set names (though there seemed to be some discussion as to whether or not this worked for everyone.
$handle = new PDO("mysql:host=localhost;dbname=dbname",
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
The following link had some other useful information:
See here
You should define the UTF-8 charset to the mysql DB, by default it used to be latin charset , we can define the utf-8 along with table definition or on database creation,
To Table
create table `my_table` (.....
.... ) DEFAULT CHARSET=utf8;
To database
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8
correct me if 'm worng
You need to set the character set of the database to UTF-8.
Command:
ALTER DATABASE db_name CHARACTER SET utf8 COLLATE utf8_general_ci;
For alter the column:
ALTER TABLE [table_name] MODIFY
[column_name] [data_type]
CHARACTER SET utf8
COLLATE utf8_general_ci;

Convert data from latin1_swedish to utf-8

I need to convert data from old database to new one. Old database was in latin1_swedish_ci collation and have content in cyrilic language like this
<p>ÐрхиепиÑкоп охридÑки и ми...
This content with utf-8 enconding on page looks like this
<p>Архиепископ охридски и митрополит скопски ...
Which is fine. Now I need to convert all of this data into native UTF-8 content. No expirience with these, any sugg.
Thanks
You can try this
ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci
And note that , this will affect existing column collations also. If you want to change default collasion to utf8 , must change database collation. After that all new table will be utf8
From the manual,
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8;
However, if you have characters that cannot be converted then you will lose that data. First make a backup and try it there.

Database charset conversion

I've moved database from one host to another. I've used PMA to export and bigdump to import. The whole database have latin2 charset set everywhere where it's possible. However in database, special chars (polish ąęłó, etc.) are broken. When I used SELECT i see "bushes" - "Ä�" insetad of "ą". Then I've set document encoding to utf-8... And the characters are good. How to fix this? Can it be done using CONVERT in query? I don't want to export/import database again, because it has over 200MB. What's wrong?
Every PHP/MySQL query solution will save me.
Sorry if you can't understand this, because I'm still learning english though.
If a table contains the wrong kind of charset (let's say utf-8 has slipped into latin1 column varhcar(255)):
ALTER TABLE tablename MODIFY colummname BINARY(255);
ALTER TABLE tablename MODIFY colummname VARCHAR(255) CHARSET utf8;
ALTER TABLE tablename MODIFY colummname VARCHAR(255) CHARSET latin1;
See also: http://dev.mysql.com/doc/refman/4.1/en/charset-conversion.html
However, it is more likely you just have a wrong character set in your default connection. What does a SET NAMES latin1; before selecting result in?

Categories