William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Tag Archives: mdb2

MDB2: Common functions summary

As PEAR’s core libary, MDB2 has hundreds of functions. Sometimes easily to be confused. e.g, queryOne(), fetchOne(), getOne(), they seem to be very similiar. How to distinguish them, and when to use them?

PHP Pear’s MDB2 has not much documents and examples, here I list 2 are very useful references:

I also extract and summarize the functions which are most often used, easy confusion:

1. querying

function describe
query in file MDB2.php, method MDB2_Driver_Common::query()
Send a query to the database and return any results
queryAll in file MDB2.php, method MDB2_Driver_Common::queryAll()
Execute the specified query, fetch all the rows of the result set into a two dimensional array and then frees the result set.
queryRow in file MDB2.php, method MDB2_Driver_Common::queryRow()
Execute the specified query, fetch the values from the first row of the result set into an array and then frees the result set.
queryCol in file MDB2.php, method MDB2_Driver_Common::queryCol()
Execute the specified query, fetch the value from the first column of each row of the result set into an array and then frees the result set.
queryOne in file MDB2.php, method MDB2_Driver_Common::queryOne()
Execute the specified query, fetch the value from the first column of the first row of the result set and then frees the result set.

2. results

function describe
fetchAll in file MDB2.php, method MDB2_Result_Common::fetchAll()
Fetch and return all rows from the current row pointer position
fetchRow in file MDB2.php, method MDB2_Result_Common::fetchRow()
Fetch and return a row of data
fetchCol in file MDB2.php, method MDB2_Result_Common::fetchCol()
Fetch and return a column from the current row pointer position
fetchOne in file MDB2.php, method MDB2_Result_Common::fetchOne()
fetch single column from the next row from a result set

3. gets

function describe
getAll in file Extended.php, method MDB2_Extended::getAll()
Fetch all the rows returned from a query.
getRow in file Extended.php, method MDB2_Extended::getRow()
Fetch the first row of data returned from a query. Takes care of doing the query and freeing the results when finished.
getCol in file Extended.php, method MDB2_Extended::getCol()
Fetch a single column from a result set and return it as an indexed array.
getOne in file Extended.php, method MDB2_Extended::getOne()
Fetch the first column of the first row of data returned from a query.
getColumnNames in file MDB2.php, method MDB2_Result_Common::getColumnNames()
Retrieve the names of columns returned by the DBMS in a query result or from the cache.

4. misc

function describe
numCols in file MDB2.php, method MDB2_Result_Common::numCols()
Count the number of columns returned by the DBMS in a query result.
numRows in file MDB2.php, method MDB2_Result_Common::numRows()
Returns the number of rows in a result object
escape in file MDB2.php, method MDB2_Driver_Common::escape()
Quotes a string so it can be safely used in a query. It will quote the text so it can safely be used within a query.
exec in file MDB2.php, method MDB2_Driver_Common::exec()
Execute a manipulation query to the database and return the number of affected rows
rowCount in file MDB2.php, method MDB2_Result_Common::rowCount()
Returns the actual row number that was last fetched (count from 0), used in CRUD.
lastInsertID in file MDB2.php, method MDB2_Driver_Common::lastInsertID()
Returns the autoincrement ID if supported or $id or fetches the current ID in a sequence called: $table.(empty($field) ? ” : ‘_’.$field)
isError in file MDB2.php, method MDB2::isError()
Tell whether a value is a MDB2 error.

PEAR: MDB2 2 tips

1. xampp default not install PEAR::MDB2

In Windows 7, apachefriends’s XAMPP defaultly doesn’t install PEAR’s MDB2. I checked the phpinfo(), no PEAR’s MDB2. very simple to install it:

C:\xampp\php>pear install MDB2-2.5.0b3

After installation, the MDB2 is auto extracted in C:\xampp\php\PEAR\ directory (which is available in php.ini’s include_path options). Restart XAMPP, the MDB2 is accessible. Don’t need ini_set(‘include_path’, PEAR_MDB2_PATH) to manually set.

2. Remove Deprecated messages.

if using report error messages in PHP code:

error_reporting(E_ALL);

All error/warnings information will be display on the screen. For the latest MDB2, it keeps warnings like this:

Deprecated: Assigning the return value of new by reference is deprecated …

The reason is that in PHP5 the =& is deprecated.

$mdb2 =& MDB2::factory($dsn, $options);

It is not required and should be removed, like:
$mdb2 = MDB2::factory($dsn, $options);

Except my code, some extra steps need to do to clear all the warnings: because the original MDB2 files are using this way:

  1. C:\xampp\php\PEAR\MDB2.php
  2. C:\xampp\php\PEAR\MDB2\Driver\mysqli.php

Change all the =& to = in this 2 files. This way will remove all the ‘Deprecated’ warning messages from screen.

My PHP version is 5.3.1, and MDB2 version is ‘2.4.1’.