db_name和=之间的空格在PDO中有什么区别

This is the code when I try connecting to database. I have intentionally given wrong database name.

<?php
   try 
    {
        $pdo = new PDO('mysql:host=localhost;dbname=ehrp', 'root', '');

    }   

        catch (PDOException $e) 
    {
        echo $e->getMessage();

    }

?>

This is the exception that I get, which is fine : SQLSTATE[HY000] [1049] Unknown database 'ehrp'

But when I write this : $pdo = new PDO('mysql:host=localhost;dbname =ehrp', 'root', '');

Notice a space between dbname and = I get nothing on screen.No error is shown. Why is that?

The argument you're passing to the PDO constructor is in URI form, and a URI cannot contain arbitrary spaces, they all mean something. So what you've actually supplied the PDO constructor as far as it can tell is mysql:host=localhost; followed by an assigned property called "database " (with a space at the end). Since PDO does not know anything about a property called "database " there are no errors (it's a legal URI property value assignment), and as an unknown property, it just gets ignored. No errors, no warnings, you've done nothing wrong and PDO does what you ask it do to.

The PDO connection to your server on localhost succeeds, and you now have a PDO instance that is connected, and simply has not been tied to a specific database yet.

To verify this, prepare a SELECT DATABASE() statement with your PDO object, and then execute it: it should work just fine, and come back with a response indicating that you're not connected to a database yet.