I am trying to get Doctrine2 to work properly with a mysql db in Symfony2 on a debian squeeze system.
The charset and collation of the DB are set to utf8 and utf8_general_ci and the db is created manually by reading in an sql script.
Now I enter some values for one of the tables with danish special chars like æøå and they simply wont display correctly.
My parameters.yml has set encoding: utf8 and in my config.yml the doctrine configuration has:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
When I do a select in the mysql console, the characters display correctly and also if I try by replacing the output array in the controller by som hardcoded values, so the problem seems definately to stem from some kind of error/bug in doctrine2 configuration...
Does anybody have any clues on how to solve this?
Thanks a lot.
Ok I finally figured it out... there is some kind of double encoding going on with this configuration. Commenting out charset: UTF8 from doctrine dbal config solves the problem.
the problem might be in your application charset. it can be found in config.yml
framework:
charset: UTF-8
#translator: { fallback: lt }
#secret: %secret%
#other options
Related
In database.yml i have this configuration:
development:
adapter: mysql2
encoding: utf8
...
But ActiveRecord::Base.connection.collation returns latin1_swedish_ci this is not what i expected, because default collation is utf8_unicode_ci. How can i get default collation in rails?
As per my knowledge you can check it on two places, remember you have also to check your mysql database. For ruby on rails you can check configuration and add as per following at end of your database.yml file
encoding: utf8mb4
collation: utf8mb4_unicode_ci
Restart server and now it should be following this configuration. Than check with following command
ActiveRecord::Base.connection.collation
Now let us check on mysql
mysql> show variables like 'collation%';
Right now I can't check these things as mysql is not setup, so will edit later to confirm you all.
I know that many had this problem, and solved it, but even with their solutions, I've gotten no luck..
I tried exporting my localhost phpadmin to my webserver, and i got:
SQL query:
/*!40101 SET NAMES utf8mb4 */;
MySQL said: Documentation
#1115 - Unknown character set: 'utf8mb4'
Local phpmyadmin:
Server type: MariaDB
Server charset: UTF-8 Unicode (utf8)
Version: 4.5.2
Webhost phpmyadmin:
Version: 2.11.4
Solved it. Downloaded MYSQL workbench, logged into my local database, and exported it from the program. Then, i simply imported the file, and it worked!
First of all hello everyone !
I'm on a Symfony 1.4 website, and I don't really have knowledge in this framework.
The site already uses 2 database connections, in databases.yml
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=localhost;dbname=my_db_name
username: my_username
password: ***
charset: utf8
encoding: utf8
attributes:
default_table_collate: utf8_general_ci
default_table_charset: utf8
use_native_enum: true
# Base de test
test:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=localhost;dbname=my_other_db_name
username: my_username
password: ***
charset: utf8
encoding: utf8
attributes:
default_table_collate: utf8_general_ci
default_table_charset: utf8
use_native_enum: true
It's working I think, well I never use base test but I guess it works.
I need to use another database, not in localhost this time but from another server.
I tried so many thing, that i learn from stackoverflow post.
I created a config folder into my module, where I put the code into a new databases.yml
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=mysql_host.bdb;dbname=mysql_db_name
username: mysql_username
password: ***
charset: utf8
encoding: utf8
attributes:
default_table_collate: utf8_general_ci
default_table_charset: utf8
use_native_enum: true
I don't know how to access to this database, seems to not working at all.
So I try directly into my template, seems logic because I need this database only into 1 template, in response of a form.
$bd_nom_serveur='mysql_host.bdb';
$bd_login='mysql_user';
$bd_mot_de_passe='***';
$bd_nom_bd='mysql_db_name';
$base = mysqli_connect($bd_nom_serveur, $bd_login, $bd_mot_de_passe, $bd_nom_bd);
Sending me warning
Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'mysql_host.bdb' (2) in /srv/d_/www/draft.site.com/apps/frontend/modules/FormContactSlot/templates/sendMailCatalog.php on line 14
I also tried
$dsn = 'mysql:dbname=mysql_db_name;host=mysql_host.bdb';
$user = 'mysql_user';
$password = '***';
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
sendig me error
500 | Internal Server Error | PDOException SQLSTATE[HY000] [2005]
Unknown MySQL server host 'mysql_host.bdb' (2)
I try with and without .bdb but nothing change.
I don't know how to access to this database from symfony 1.4
Can anyone help me please !
keep in mind that I have very little knowledge of Symfony, be gentle please !
EDIT :
I also tried another trick.
I add the database after my principal database on config/databases.yml
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=localhost;dbname=my_db_name
username: my_username
password: ***
charset: utf8
encoding: utf8
attributes:
default_table_collate: utf8_general_ci
default_table_charset: utf8
use_native_enum: true
client:
class: sfDoctrineDatabase
param:
dsn: mysql:host=host.bdb;dbname=dbname
username: user
password: ***
charset: utf8
encoding: utf8
attributes:
default_table_collate: utf8_general_ci
default_table_charset: utf8
use_native_enum: true
And this time I got an error whenever I try to go on my website. I also do the command symfony doctrine:build-schema but this send me the same error unknown mysql server...
Please I need help, I have to access my mysql database from symfony, I don't have the choice.
I respond to myself because I find a solution :
There is no solution.
I'm on a shared OVH solution, and shared OVH databases are not remotely accessible. So I have to take a vps solution, where there is IP host for database.
Thanks to Marek for helping me finding the solution.
Here the query that works.
$bd_nom_serveur='IP host';
$bd_login='mysql_user';
$bd_mot_de_passe='***';
$bd_nom_bd='mysql_db_name';
$base = mysqli_connect($bd_nom_serveur, $bd_login, $bd_mot_de_passe, $bd_nom_bd);
PDO still not working.
MySQL 5.5.35
Django 1.6.1
In order to support emoticons in the DB, I have configured in my django settings:
'OPTIONS': {'charset': 'utf8mb4'}
On MySQL connection, I get this error:
LookupError: unknown encoding: utf8mb4
How should I configure Django/MySQL in order to support utf8mb4?
https://code.djangoproject.com/ticket/18392#comment:10
As a workaround, you can make python understand 'utf8mb4' as an alias
for 'utf8':
import codecs
codecs.register(lambda name: codecs.lookup('utf8') if name == 'utf8mb4' else None)
If you really need utf8mb4,
follow the steps in https://mathiasbynens.be/notes/mysql-utf8mb4, and
make sure your python package "MySQL-python" version is >= 1.2.5 !
Fast and easy way.
connection = mysql.connector.connect(user='username',
password='mypass',
host='localhost',
database='mydb',
use_pure=True,
charset='utf8'
)
This worked in my case (MySQL 5.7 + Django 3.1):
Configure Django:
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'charset': 'utf8mb4'
}
Configure MySQL database
make sure all db tables are using InnoDB storage engine (this is important; the next step will probably fail if you skip it)
change the Collation for all your tables to utf8mb4_general_ci
In my database config I have:
adapter: mysql
database: xxx
username: xxx
password: xxx
host: localhost
encoding: 'latin1'
My table definition has: DEFAULT CHARSET=latin1
I put this at the top of my file:
# encoding: iso-8859-1
And yet still when I save my record and load it again the latin1 data has been mysteriously converted to utf8. Can someone suggest something I'm missing?
What about the columns' encoding? You have to set the connection, the tables, the columns and the program encoding/charset to the same one to have your code work properly.