We can execute queries in the following ways using PDO . I preferred to use prepare method.
> Query Method
> Execute Method
> Prepare Method

Here we can take an example to delete 3rd record from departments table.

Establish a DB connection.
$conn = new PDO(‘mysql:host=localhost;dbname=demo’, ‘username’, ‘password’);

Query Method
Syntax: $conn->query();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Execute Method
Syntax: $conn->exec();
Parameter: SQL Statement
Success: Number of rows
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Prepare Method
Syntax: $conn->prepare();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Advantages:
Here we can not pass the value directly in SQL statement instead use named parameters (:name or ?). It prevent SQL injection attacks