Good day community!

I decided to delve into the depths of php and mysql with the book "PHP and MySQL. From beginner to professional." I really like the book almost all worked, when working with a book I use XAMPP, but one nuance bothers me.

I connect to a specific database table using a script:

try { $pdo = new PDO('mysql:host = localhost; dbname = ijdb', 'ijdbuser', 'pass'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { $output = 'Невозможно подключиться к серверу баз данных! ' . $e -> getMessage(); include 'output.html.php'; exit(); } 

Which implies a connection to a specific server database, and as a result I cannot write a query of the type:

 SELECT joke.id, joketext, name, email FROM joke INNER JOIN author on authorid = author.id 

the server eats the request only if I modify it:

SELECT joke.id, joketext, name, email FROM ijdb .joke INNER JOIN ijdb .author on authorid = author.id

At the first option the error appears:

QLSTATE [3D000]: Invalid catalog name: 1046 No database selected

* How to get rid of redundancy writing!?

  • You are told that the base is not chosen, and where is the redundancy of writing ? - Alexey Shimansky
  • 2
    new PDO ('mysql: host = localhost; dbname = ijdb ', 'ijdbuser', 'pass') - zatoexp
  • one
    I chose it, why should I write a view construct . at a choice of the table over which the request? - zatoexp
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

When connecting to a PDO, you must strictly adhere to the DSN format . In particular, do not add gag in the form of spaces, quotes and other decorations.

 $pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'pass'); 
  • one
    Thank you very much! I rummaged through a bunch of topics in the internet, your answer helped. I never would have thought that the whole thing in the gaps. I thought when processing they are removed. - zatoexp