concatenation error when instantiating PDO

0

I have the connection PDO with mysql below:

 $dsn = 'mysql:host=' . self::$hostname . '; dbname=' . self::$dbname;
 $opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');

 self::$conexao = new PDO( 
                              $dsn,
                              self::$username, 
                              self::$password, 
                              $opcoes
                          );

that works perfectly.

But if I put the content of the variable $dsn next to the string of connection as below gives error > which seems to me to be concatenation .

Does anyone please help me figure out where this error is?

$opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');

 self::$conexao = new PDO( "mysql:
                              host=" . self::$hostname . "; 
                              dbname=" . self::$dbname,
                              self::$username, 
                              self::$password, 
                              $opcoes
                          );
    
asked by anonymous 20.06.2018 / 13:00

1 answer

1

I imagine it's from the line break. try this:

new PDO( "mysql:host=".self::$hostname.";dbname=". self::$dbname, self::$username, self::$password, $opcoes);
    
20.06.2018 / 13:08