How to execute select statements

A method of the PDO class for preparing a SQL statement

      prepare($sql_statement)
      // Prepares the specified SQL statement for execution and returns a 
      // PDOStatement object. Within the SQL statement, you can use a 
      // colon (:) to add parameters to the statement.
      

Two methods of the PDOStatement class for executing a statement

      bindValue($param, $value)
      // Binds the specified value to the specified parameter in the prepared statement. 
      
      execute() 
      // Executes the prepared statement.
      

How to execute a SQL statement that doesn’t have parameters

      $query = "SELECT * FROM products";
      $statement = $db->prepare($query);
      $statement->execute();
      

How to execute a SQL statement that has a parameter

      $query = "SELECT * FROM products WHERE categoryID = :category_id";
      $statement = $db->prepare($query);
      $statement->bindValue(':category_id', $category_id);
      $statement->execute();
      

Description

Back