How to add custom mysql database in symfony - mysql

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.

Related

FreeTDS UTF-8 insert in mysql

I have a linux server with mysql with symfony framework installed.
Now i'm trying to get data from sql server 2012 and all works great except when i try to insert value in mysql database.
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x80 IN P...' for column 'note_anagrafica' at row 1
Seems that the problem is on data conversion.
The data type of column inside mysql where i'm trying to insert the data is:
text
And the collation is:
utf8_unicode_ci
FreeTds configuration is set on UTF-8 and driver is odbc.
Someone have a solution for this problem?
EDIT:
odbc.ini
[server]
Driver = FreeTDS
Server = x.x.x.x
Port = 1234
Database = databasename
TDS_Version = 8
client charseg = UTF-8
server charset = CP1252
freetds.conf
[server]
host = x.x.x.x
port = 1234
tds version = 8.0
client charset = UFT-8
text size = 20971520
use uft-16 = true
doctrine config for mysql.
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
doctrine config for external mssql database:
server:
driver_class: Lsw\DoctrinePdoDblib\Doctrine\DBAL\Driver\PDODblib\Driver
host: "%a_database_host%"
port: "%a_database_port%"
dbname: "%a_database_name%"
user: "%a_database_user%"
password: "%a_database_password%"
charset: utf8mb4
from now i'm getting error when i'm trying to insert data to mysql comes from sql server 2012 with doctrine.
Till now i have no problem on reading execpt that if i out the result on the web the special charaters like ø comes with � symbol.
There is a conversion that i have to made before call
$entityManager->persist($object);
$entityManager->flush();
Set the charset to utf8mb4 for doctrine config and will do the trick.

How to select the connection to database when I use console in symfony2

I have two databases, (MySQL and Oracle), I did the connection betweek sf2 and both databases, here is my config.yml file:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
sysman:
driver: %database_driver2%
host: %database_host2%
port: %database_port2%
dbname: %database_name2%
user: %database_user2%
password: %database_password2%
charset: UTF8
My question is, how can I run console command on the second database (Oracle), commands like (doctrine:database:create ...), and thanks
Use the --connection parameter:
php app/console doctrine:database:create --connection=default
or
php app/console doctrine:database:create --connection=sysman
You should first read a tutorial about commands and how to pass options and parameters to the commands. And how to distinguish between an option and a parameter.
If you want to make your own commands...
You will probably want to make it like this - if you do not pass an option (you will use the default database), if you pass it, you will make sure it is a valid option, and use the passed database connection name.
Doctrine is not tightly coupled with Mysql, you can use almost all most common available databases.
Also note, commands are container aware. That means you commands can access container, though which you have access to your services, such as doctrine:
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection $input->getArgument('connection');
# Validate connection argument here and use it below
$container = $this->getContainer();
$em = $container->get('doctrine')->getManager(); // default db
$em = $container->get('doctrine')->getManager('sysman'); // another
return 1;
}
I wrote the code without testing, excuse me for any mistake I might have done.
php app/console doctrine:mapping:info --em=default (same without em option)
php app/console doctrine:mapping:info --em=sysman

Doctrine2 with Symfony2 not recognizing db charset and collation

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

activerecord/mysql saves my latin1 data as utf8

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.

MySQL changes UTF-8 to ASCII-8BIT

I've this scenario.
A movie title:
$ title = "La leyenda de Osaín"
With this encoding:
$ title.encoding.name
>> UTF-8
I then saves it to the database.
$ movie = Movie.create!(:title => title)
Then I try to get the movie.
$ Movie.find(movie.id).title.encoding.name
>> "ASCII-8BIT"
$ Movie.find(movie.id).title
>> "La leyenda de Osa\xC3\xADn"
All other movies works that does not contain special characters like í and û.
This is my database.yaml file:
development:
adapter: mysql
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
I'm getting the right sort of data when using forced_encoding.
$ Movie.find(movie.id).title.force_encoding("UTF-8")
>> "La leyenda de Osaín"
I'm using Rails 3.0.5.rc1 with MySQL 14.14.
Anyone knows what the problem may be?
I found a solution to my problem.
Now I'm using the newer mysql2 gem.
I replaced gem "mysql" with gem "mysql2" inside the Gemfile.
Then I changed the database adapter inside the database.yaml file.
From:
development:
adapter: mysql
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
To:
development:
adapter: mysql2
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
I think this was the deal breaker in my case:
Taken from Github MySQL2
[...]It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9[...]
According to this link, rails scaffolding creates varchar(255) columns in mysql. The mysql documentation says the following about varchar(255):
For example, a VARCHAR(255) column can
hold a string with a maximum length of
255 characters. Assuming that the
column uses the latin1 character set
(one byte per character), the actual
storage required is the length of the
string (L), plus one byte to record
the length of the string.
My guess is that the column type in the database doesn't support characters that are represented by more than one byte. This link has more information about common pitfalls in rails when dealing with unicode strings and more specifically, it says you need to create your database as utf8 like so:
CREATE_DATABASE my_web_two_zero_development DEFAULT CHARSET utf8;