concatenation error when instantiating PDO

Asked

Viewed 25 times

0

I have the connection PDO with mysql down 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 contents of variable $dsn next to string of connection as below error which seems to me to be of concatenation.

Will someone please help me find out where that mistake 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
                          );
  • 1

    As far as I know, it is not possible to have line breaks in the DSN.

  • solved the problem?

  • yes. I have already accepted the answer below. Thank you!

1 answer

1


I’m guessing it’s the line break. try like this:

new PDO( "mysql:host=".self::$hostname.";dbname=". self::$dbname, self::$username, self::$password, $opcoes);
  • I tried it before and it didn’t work either!

  • 1

    is, in fact, I was putting the line break in all parameters. When I put only the last 2, then it worked. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.