Search code examples
mysqlsqldoctrinesymfony1symfony-1.4

How to add custom mysql database in symfony


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.


Solution

  • 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.