What is the difference between PDO’s query() vs execute()?

Technology CommunityCategory: PHPWhat is the difference between PDO’s query() vs execute()?
VietMX Staff asked 4 years ago
  • query runs a standard SQL statement and requires you to properly escape all data to avoid SQL Injections and other issues.
  • execute runs a prepared statement which allows you to bind parameters to avoid the need to escape or quote the parameters. execute will also perform better if you are repeating a query multiple times.

Best practice is to stick with prepared statements and execute for increased security. Aside from the escaping on the client-side that it provides, a prepared statement is compiled on the server-side once, and then can be passed different parameters at each execution.