Skip to content

PreparedStatement

Alexey Borzov edited this page Sep 12, 2017 · 2 revisions

PreparedStatement class

An instance of this class is returned by \sad_spirit\pg_wrapper\Connection::prepare(). It prepares a query via pg_prepare() and executes it later using pg_execute().

PreparedStatement has the following public methods:

  • bindParam(int $paramNum, mixed &$param, mixed $type = null): $this - Binds a variable to a parameter of the prepared query
    • $paramNum - 1-based parameter number
    • $param - Variable to bind
    • $type - either a TypeConverter instance or a type specification for TypeConverterFactory. Will be used to convert the parameter when executing the statement.
  • bindValue(int $paramNum, mixed $value, mixed $type = null): $this - Sets the value for a parameter of the prepared query
    • $paramNum - 1-based parameter number
    • $value - Parameter value
    • $type - either a TypeConverter instance or a type specification for TypeConverterFactory. Will be used to convert the parameter when executing the statement.
  • execute(array $params = array(), array $resultTypes = array()): ResultSet|int - Executes the prepared query returning either the number of affected rows or ResultSet depending on query
    • $params - Input parameters for query. When provided these will override bound parameters set by above methods.
    • $resultTypes - Types for output columns, will be passed to ResultSet. The array should contain either TypeConverter instances or type specifications for TypeConverterFactory and its indexes may be either names or indexes of result columns.
  • prepare(): $this - Actually prepares the statement with pg_prepare(). Automatically called by constructor.
  • deallocate(): $this - Manually deallocates the prepared statement. This is usually not needed as all the prepared statements are automatically deallocated when database connection is closed. Trying to call execute() after deallocate() will result in an exception.