MDB2-Cheatsheet
2006-03-06 02:31:24When I started using MDB2, I found the possibilities quite overwhelming and so I decided to summarize everything you may need to start using it or want as a reference if you already use it and don't want to dig through the manuals for everything. Questions and hints on clarification are appreciated.
init a connection
$dsn = 'mysql://user:pass@host/db';
$mdb2 =& MDB2::factory(mixed $dsn, [array $options = false]);
if (PEAR::isError($mdb2)) {
echo ($mdb2->getMessage());
}
querying
docs - query//Send a query to the database and return any results
$result = $mdb2->query(string $query, [mixed $types = null]);
//Execute the specified query, fetch all the rows of the result set into a two dimensional array and then frees the result set.
$result = $mdb2->queryAll(string $query, [array $types = null], [int $fetchmode = MDB2_FETCHMODE_DEFAULT], [boolean $rekey = false]);
//$rekey = if set to true, the $all will have the first column as its first dimension
//Execute the specified query, fetch the values from the first row of the result set into an array and then frees the result set.
$result = $mdb2->queryRow($string $query, [string $type = null]);
results
docs - setresultTypes//Define the list of types to be associated with the columns of a given result set.
$types = array('integer', 'text', 'timestamp');
$result->setResultTypes($types)
//Fetch and return a row of data
$result->fetchRow([int $fetchmode = MDB2_FETCHMODE_DEFAULT], [int $rownum = null]);
//Fetch single column from the first row from a result set
$result->fetchOne([int $colnum = 0]);
//Fetch and return a column of data (it uses fetchRow for that)
$result->fetchAll([int $fetchmode = MDB2_FETCHMODE_DEFAULT], [boolean $rekey = false]);
//$rekey = if set to true, the $all will have the first column as its first dimension
//Fetch and return a column of data (it uses current for that)
$result->fetchCol([int $colnum = 0]);
//Retrieve the names of columns returned by the DBMS in a query result or from the cache.
$result->getColumnNames();
//Count the number of columns returned by the DBMS in a query result.
$result->numCols();
//returns the number of rows in a result object
$result->numRows();
//seek to a specific row in a result set
$result->seek($int);
//Move the internal result pointer to the next available result
$result->nextResult();
//Free the internal resources associated with result.
$result->free();
fetch modes
MDB2_FETCHMODE_DEFAULT
MDB2_FETCHMODE_ORDERED
MDB2_FETCHMODE_ASSOC
MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED
MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED
MDB2_FETCHMODE_OBJECT
security
$mdb2->quote($stuff, $mode);
//text, integer, boolean, decimal, float, date, time, timestamp, blob
misc
docs - lastInsertID//returns the autoincrement ID if supported or $id
$mdb2->lastInsertID();
$mdb2->nextID();
About
Life's a bitch, life's a whore. Nothing less, nothing more.