Connecting to Database using PDO
The format is like this
Here is an example with values
In database details
mysql: tells what database engine to use.
Exceptions happen when something goes wrong. Any code connecting to an external system needs to handle exceptions.
To handle exceptions in PHP we use
Exception is actually an object instantiating PHP’s native exception class. The variable
$e will contain the details of the exception. After the exception, stop any more code from executing using the
It’s a good idea to use a separate
try block for each point of interaction.
Querying the Database
To call a method on an object, you first specify the object name followed by a single arrow
->. After that you specify the method name.
exec is short for execute SQL command. This method is used to run a sql command on the database. Methods are like functions, they receive arguments.
SET NAMES defines the character set, in this case utf8
Handling Exception with PDO
The command above will throw an exception when there is an error in the query.