How does pdo work in php?

PHP Data Objects

  • Introduction
  • Installing/Configuring
    • Requirements
    • Installation
    • Runtime Configuration
    • Resource Types
  • Predefined Constants
  • Connections and Connection management
  • Transactions and auto-commit
  • Prepared statements and stored procedures
  • Errors and error handling
  • Large Objects [LOBs]
  • PDO — The PDO class
    • PDO::beginTransaction — Initiates a transaction
    • PDO::commit — Commits a transaction
    • PDO::__construct — Creates a PDO instance representing a connection to a database
    • PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
    • PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle
    • PDO::exec — Execute an SQL statement and return the number of affected rows
    • PDO::getAttribute — Retrieve a database connection attribute
    • PDO::getAvailableDrivers — Return an array of available PDO drivers
    • PDO::inTransaction — Checks if inside a transaction
    • PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
    • PDO::prepare — Prepares a statement for execution and returns a statement object
    • PDO::query — Prepares and executes an SQL statement without placeholders
    • PDO::quote — Quotes a string for use in a query
    • PDO::rollBack — Rolls back a transaction
    • PDO::setAttribute — Set an attribute
  • PDOStatement — The PDOStatement class
    • PDOStatement::bindColumn — Bind a column to a PHP variable
    • PDOStatement::bindParam — Binds a parameter to the specified variable name
    • PDOStatement::bindValue — Binds a value to a parameter
    • PDOStatement::closeCursor — Closes the cursor, enabling the statement to be executed again
    • PDOStatement::columnCount — Returns the number of columns in the result set
    • PDOStatement::debugDumpParams — Dump an SQL prepared command
    • PDOStatement::errorCode — Fetch the SQLSTATE associated with the last operation on the statement handle
    • PDOStatement::errorInfo — Fetch extended error information associated with the last operation on the statement handle
    • PDOStatement::execute — Executes a prepared statement
    • PDOStatement::fetch — Fetches the next row from a result set
    • PDOStatement::fetchAll — Fetches the remaining rows from a result set
    • PDOStatement::fetchColumn — Returns a single column from the next row of a result set
    • PDOStatement::fetchObject — Fetches the next row and returns it as an object
    • PDOStatement::getAttribute — Retrieve a statement attribute
    • PDOStatement::getColumnMeta — Returns metadata for a column in a result set
    • PDOStatement::getIterator — Gets result set iterator
    • PDOStatement::nextRowset — Advances to the next rowset in a multi-rowset statement handle
    • PDOStatement::rowCount — Returns the number of rows affected by the last SQL statement
    • PDOStatement::setAttribute — Set a statement attribute
    • PDOStatement::setFetchMode — Set the default fetch mode for this statement
  • PDOException — The PDOException class
  • PDO Drivers
    • CUBRID [PDO] — CUBRID Functions [PDO_CUBRID]
    • MS SQL Server [PDO] — Microsoft SQL Server and Sybase Functions [PDO_DBLIB]
    • Firebird [PDO] — Firebird Functions [PDO_FIREBIRD]
    • IBM [PDO] — IBM Functions [PDO_IBM]
    • Informix [PDO] — Informix Functions [PDO_INFORMIX]
    • MySQL [PDO] — MySQL Functions [PDO_MYSQL]
    • MS SQL Server [PDO] — Microsoft SQL Server Functions [PDO_SQLSRV]
    • Oracle [PDO] — Oracle Functions [PDO_OCI]
    • ODBC and DB2 [PDO] — ODBC and DB2 Functions [PDO_ODBC]
    • PostgreSQL [PDO] — PostgreSQL Functions [PDO_PGSQL]
    • SQLite [PDO] — SQLite Functions [PDO_SQLITE]

djlopez at gmx dot de

16 years ago

Please note this:

Won't work:
$sth = $dbh->prepare['SELECT name, colour, calories FROM ?  WHERE calories < ?'];

THIS WORKS!
$sth = $dbh->prepare['SELECT name, colour, calories FROM fruit WHERE calories < ?'];

The parameter cannot be applied on table names!!

wiserufferto at gmail dot com

1 year ago

This is a little late... but I'm old and slow.......
Regarding Extending PDOStatement and PDO I found that sending the PDOExtended class by reference helps:
    In the constructor after parent::__construct[] :
$this->setAttribute[\PDO::ATTR_STATEMENT_CLASS,array['PDOStatementExtended', [&$this]]];}

And in
class PDOStatementExtended extends \PDOStatement
{

    protected function __construct
  [
    \PDO &$PDO,
   ]

pokojny at radlight dot com

16 years ago

I wanted to extend PDO class to store statistics of DB usage, and I faced some problems. I wanted to count number of created statements and number of their executings. So PDOStatement should have link to PDO that created it and stores the statistical info. The problem was that I didn't knew how PDO creates PDOStatement [constructor parameters and so on], so I have created these two classes:



Please note the inclusion of buffer control. I only needed this when using 'include','include_once','require', or 'require_once' - my feeling is there is a subtle issue with those options as even an empty include file caused a buffer issue for me. === AND YES, I DID CHECK MY INCLUDE FILES DID NOT HAVE SPURIOUS WHITESPACE ETC OUTSIDE THE DELIMITERS! ===

www.navin.biz

16 years ago

Below is an example of extending PDO & PDOStatement classes:

neonmandk at gmail dot com

14 years ago

If you will make a OBJ row from PDO you can use this eg.

$resKampange = $dbc->prepare[ "SELECT * FROM Table LIMIT 1" ];
$resKampange->execute[];
$rowKampange = $resKampange->fetch[ PDO::FETCH_OB ];

echo $rowKampange->felt1;

Good lock :0]

matthew at button-mashers dot net

8 years ago

When using prepared statements there is no official PDO feature to show you the final query string that is submitted to a database complete with the parameters you passed.

Use this simple function for debugging. The values you are passing may not be what you expect.

paulius_k at yahoo dot com

16 years ago

If you need to get Output variable from MSSQL stored procedure, try this :

-- PROCEDURE
CREATE PROCEDURE spReturn_Int @err int OUTPUT
AS
SET @err = 11
GO

$sth = $dbh->prepare["EXECUTE spReturn_Int ?"];
$sth->bindParam[1, $return_value, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT];
$sth->execute[];
print "procedure returned $return_value\n";

lkmorlan at uwaterloo dot ca

11 years ago

You may also have to edit /etc/freetds.conf. Make sure the TDS version is recent, e.g., "tds version = 8.0".

Sbastien Gourmand

9 years ago

Merge the prepare[] and execute[] in one function like a sprintf[].
And like sprintf, I choose to use unnamed args [?] ;]

you could still use old insecure query[] [ not prepared ] with renamed function :]

Chủ Đề