3 Replies - 292 Views - Last Post: 11 February 2018 - 02:40 AM Rate Topic: -----

#1 tedchong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 31-December 17

Single code in the string to set up DSN

Posted 10 February 2018 - 10:10 PM

To set up the DSN while connect to database,
$dsn = 'msql:host='.$host.';dname='.$dname;
What is the meaning of the single code(.)? And why the variable $host need 2 single code on left and right of the variable and variable $dname only need one single code?
Any reference website can provide to me?
Is This A Good Question/Topic? 0
  • +

Replies To: Single code in the string to set up DSN

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5256
  • View blog
  • Posts: 14,069
  • Joined: 18-April 07

Re: Single code in the string to set up DSN

Posted 10 February 2018 - 10:54 PM

That is essentially a connection string. It tells PDO where to find the host and database to login to. You have the single quotes on the left and right because it is concatenating the variables and strings together into one string. You don't have to do it that way. You could have also done "msql:host=$host;dname=$dname";. The method is used just like any other string concatenation. For example $myvar = 'this is ' . 'a string';

Now since they used single quotes (which doesn't evaluate variables) they are forced to do it through concatenation. Notice how my example of the string uses double quotes. When I use double quotes, variables inside the string are evaluated.

All this is covered in string concatenation...

https://secure.php.n...tors.string.php

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,715
  • Joined: 12-December 12

Re: Single code in the string to set up DSN

Posted 11 February 2018 - 02:03 AM

If you aren't aware of the basic concatenation operator, the dot, then it is apparent that you need to take a PHP fundamentals course or read a beginners book. This is essential programming knowledge.
Was This Post Helpful? 1
  • +
  • -

#4 tedchong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 31-December 17

Re: Single code in the string to set up DSN

Posted 11 February 2018 - 02:40 AM

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1