What does this mean in PDO? This horizontal bar $ pdo-> and two colons PDO: What does it mean?
Do you want to write errors like this? where can I write other code?
Try {
$ Pdo = new PDO ('MySQL: host = localhost; dbname = t2', 'root ','');
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );
}
Catch (PDOException $ e ){
Echo "database connection failed:". $ e-> getMessage ();
Exit;
}
?>
Reply to discussion (solution)
Static variables of PDO
Static variables of PDO
PDO: Call the static property of the PDO class.
-> Here, it indicates the properties and methods of the called object.
Let's take a look at the php object section below.
First, popularize the basic knowledge:
->Indicates the object member (attributes and methods)
::Represents the static members (attributes and methods) of a class)
PDO has two error handling methods:
1. in the traditional method, check the returned value of the errorCode method to determine whether an error has occurred. The errorInfo method returns the complete error message.
2. exception throw method. In your example
As an exception, new PDO supports both methods, that is
New PDO (.....);
And
Try {
New PDO (.....);
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
}
The results are the same.
Where can I write other code when exception handling is used?
The answer is: in the try branch
Any statement that has a problem will go to the catch branch.
Object instance
Static Member variable attributes
In short, the constants defined in the PDO class are generally capitalized.