I am working on multisite language that is in yii2, I have chinese and japanese language in it, When i insert record with chinese or japanese it is added with ????? , i have added
header('Content-Type: text/html; charset=UTF-8');
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
in layouts/main.php file but still in database added character as ????,
I have other solution to change charset to utf-8 for each column in database But this will be take too much time
Is there any simple way to solve this ?
Configure db in yii to use utf-8 character set.
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
Go to your phpmyadmin selected the table where you want chinese and japnense words to be inserted and set the property 'collation' to utf8_general_ci and now you can insert those words and it will get stored in database as chinese itself.
Related
I want to store Hindi character in mysql database using laravel application
I have below properties with database
Database charset = utf8
Database collation = utf8_general_ci
and these setting with config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'db'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
but when i try to store data it gives me this error
"message": "SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '=' (SQL: select * from `col` where (`name` = \u091b\u0924\u094d\u0924\u094b\u0917) limit 1)",
"exception": "Illuminate\\Database\\QueryException",
please suggest me something, i don't understand what i am missing
As hinted in the error message, latin1 is showing up somewhere. It is probably the default setting for the client.
Your config/database.php looks OK. So let's tackle it a different way:
Right after connecting, perform the SQL command SET NAMES utf8.
I can't tell where \u091b\u0924\u094d\u0924\u094b\u0917 is coming from, but you should really use UTF-8 encoding in your client, not that Unicode equivalent.
I was setting Charset utf8 and Collation utf8_general_ci for Database and Table
but we need to check if table columns have the same Charset and Collation or not, i was missing the same or i didn't knew about that :)
After searching for a suitable answer, I find that the solutions people offer do not work so I am posting here in the hope of some help.
I have a database of users that will list by first letter of name in a directory. However there is one user that breaks the results because their name begins with a letter with a foreign character, namely Á.
I get the user results with:
$users = $this->users->getAll($params);
And then return the response using collection which json encodes it:
return response()->collection($users);
But I get the following error if the above user is returned in results:
The Response content must be a string or object implementing
__toString(), 'boolean' given.
I've tried using the JSON_UNESCAPED_UNICODE option, like many people suggest in solutions, like this:
return response()->json($users, 200, ['Content-type'=> 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);
but it turns out the response isn't coming back as UTF-8 after attempting to log it. However, as far as I can tell, the database is UTF-8 encoded so I don't understand what's happening here.
The problem is, even if I was to change the character to a regular A, there could still be future users with foreign characters in their name, and I'd rather there be something in place to handle this.
Any help is appreciated.
collation utf8mb4_unicode_ci works fine for me.
If you mention your database collation it's define your issue more readable.
In config/database.php :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
And now make the columns collation as : utf8mb4_unicode_ci.
Hope this works. Thanks.
(Never write software when you are very tired. Otherwise you ask very nice people on the internet to answer very obvious questions, that you would not ask, if you were not tired, because the principle problem is ... trivial. lol. So, in this case, I had neglected to uniquely rename the connection properties of the alternate database. Sigh....)
PROBLEM:
I can't make Laravel recognize a request for a different connection (database).
As far as I know, I've correctly configured .ENV, config/database.php, config/app.php correctly, and added $connection=myconnection to my models.
But no matter what I do Laravel/Eloquent/Doctrine Ignores all attempts to have some models use the default (common) database, and others use the multi-tenant databases.
Let's distinguish between a physical server, a database server instance running on that server, and a database within that instance, and multi-tenancy within that database.
I should be able to use any of these configurations by modifying the connection at runtime:
Single app/db server, single app, single database, with multi-tenants
Single app/db server, single app, multiple databases, with multi-tenants
Single app/db server, single app, multiple database instances, each with multiple databases, with multi-tenants
Single app server, multiple database servers, multiple databases instances, each with multi-tennants.
Multiple app servers, multiple database servers, multiple databases instances, each with multi-tennants.
And any combination thereof.
And I suspect that the lack of documentation, yet the inclusion of the eloquent/model feature "$connection=connection_configuration" means there is a fairly obvious solution that I don't intuit. (and I don't have a few free days to delve into the source and figure out what's going on there.)
Thanks for any help.
-Cheers
===ENV FILE===
DB_CONNECTION=crunch
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crunch
DB_USERNAME=myapp
DB_PASSWORD=Nonsense!Talk
DB_CONNECTION=munchdata
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=munchdata
DB_USERNAME=myapp
DB_PASSWORD=Nonsense!Talk
===CONFIG/DATABASE.PHP===
'connections' => [
'crunch' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'crunch'),
'username' => env('DB_USERNAME', 'crunch'),
'password' => env('DB_PASSWORD', 'Nonsense!Talk'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'sticky' => true,
],
'munchdata' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'munchdata'),
'username' => env('DB_USERNAME', 'munchdata'),
'password' => env('DB_PASSWORD', 'Nonsense!Talk'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'sticky' => true,
],
],
===CONFIG/APP.PHP FILE===
'default' => env('DB_CONNECTION', 'crunch'),
// Default connections work fine
===A MODEL===
class EntityType extends BaseModel
{
use SoftDeletes;
public $connection = 'munchdata';
public $table = 'entity_type';
( ... )
}
// Ignores $connection
===CONSOLE===
>>> DB::connection('crunch')->getDatabaseName()
=> "crunch"
// Uses default connection
>>> DB::connection('munchdata')->getDatabaseName()
=> "crunch"
>>>
// Ignores connection
$result = DB::connection('munchdata')->select('select * from entity_type');
// Ignores the connection, and returns data from default db
$result = DB::connection('munchdata')->select('select munchdata.entity_type.* from munchdata.entity_type');
// Ignores the connection, but returns the right data anyway....
The values in the .ENV file must be unique, because the value "DB_CONNECTION=myDatabaseName" is not an array keyed from "myDatabaseName", but a 'dumb' string value. Meaning all entries in .ENV files must be uniquely named.
In other words, the .ENV files work OPPOSITELY from the Config/App > Connection[] definitions. Which... doesn't make a lot of sense, and isn't necessary.
I'd assumed that .ENV file was brought into an array with myDatabaseName as the key, and that the "DB_*" strings were constants. This seems logical.
This is probably how it SHOULD work, but it is not how it DOES work.
I suppose that since I've seen many other people post questions because of similar suppositions, that either we should request the change, or that I should write a bit of code to allow for multiple configs using the same constants as keys.
SOLUTION:
(1) If you have a small number of relatively invariant databases, continue to use the .ENV file - and give each CONSTANT a different name.
(2) If you dynamically create new databases, use .ENV for your default (boot) database, and then:
Either (a) Use your default (boot) database to store the related database connection configurations and cache them - which exposes your connection data and forces you to replicate that data for new instances,
Or (b) Use the file system to store the related database configurations, and (c) cache them.
In my case I have N (a lot) of databases, and I prefer using the db and then replication (dump and load the table with connection info) rather than a file of unknown size with the chance of 'leaking' into the real world due to dev/IT error or malice...
Cheers
We started adding Chinese language support to our application. Our DB Charset is set to UTF8 and I can successfully query from MySQL CLI and see the word in Chinese characters
However, when I try to use CakePHP query method "findAll", the value is returned as ?? instead of the Chinese characters
Our DB config in database.php looks like this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '******',
'database' => '******',
'prefix' => '******',
'encoding' => 'utf8'
);
Also we have this line set in core.php:
Configure::write('App.encoding', 'UTF-8');
We don't have any views set for this, we are using REST APIs so we are mainly just doing an "echo" in the Controller method for whatever response we get from the DB.
I also tried to create a test file and write this code in it:
<?php echo '基本' ?>
And it worked fine.
Any suggestions on how to get this to work?
Turned out that I needed to install php-mbstring which wasn't installed
Once I installed it and restarted apache, it worked like a charm
I have a zend application currently not displaying utf-8 encrypted special characters.
I need to use the command set name utf-8 for my mysql connection.
Col. Shrapnel hinted me with this:
$params = array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname',
'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
);
How do I get
'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
in my application.ini?
It was:
resources.db.params.charset = "utf8"
(Added in the config file)
Thanks to Col. Shrapnel again.
Try with:
database.params.driver_options.PDO::MYSQL_ATTR_INIT_COMMAND=SET NAMES UTF8;