Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Monday, July 16, 2007

PHP PEAR: MDB2

The PEAR DB class has been deprecated and replaced with MDB2.

MDB2 doesn't come with the MySQL driver out to the box, so you need to install that separately:


$ pear install MDB2
$ pear install MDB2_Driver_mysql


Here's a simple select, using the default fetchmode of MDB2_FETCHMODE_ORDERED.


<?
require_once "MDB2.php";

$dsn = 'mysql://my_username:@my_password@127.0.0.1/my_db';

$options = array(
'debug' => 2,
'result_buffering' => false,
);

$mdb2 =& MDB2::factory($dsn, $options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}

// Proceed with a query...
$res =& $mdb2->query('SELECT * FROM my_table');

// Always check that result is not an error
if (PEAR::isError($res)) {
die($res->getMessage());
}

// Iterate through result set (assume default fetchmod is MDB2_FETCHMODE_ORDERED)
while (($row = $res->fetchRow())) {
echo $row[0] . " " . $row[1] . "<br/>\n";
}

?>


The three possible fetchmode values are:
1) MDB2_FETCHMODE_ORDERED (query results returned as array - column numbers are keys)
2) MDB2_FETCHMODE_ASSOC (query results returned as hash/associative array - column names are keys)
3) MDB2_FETCHMODE_OBJECT (query results returned as object - column names as properties)


To change the default fetchmode and use MDB2_FETCHMODE_ASSOC instead, use the setFetchMode method:


$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);


and then iterate through the results:


while ($row = $res->fetchRow()) {
echo $row['column_name_a'] . ' ' . $row['column_name_b'] . "<br/>\n";
}


Or you can change it per call:


while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
echo $row['client_abbrv'] . ' ' . $row['client_name'] . "<br/>\n";
}


Here's an example of the first method:


<?
require_once "MDB2.php";

$dsn = 'mysql://my_username:@my_password@127.0.0.1/my_db';

$options = array(
'debug' => 2,
'result_buffering' => false,
);

$mdb2 =& MDB2::factory($dsn, $options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}

// Set the fetchmode
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);

// Proceed with a query...
$res =& $mdb2->query('SELECT * FROM my_table');

// Always check that result is not an error
if (PEAR::isError($res)) {
die($res->getMessage());
}

// Iterate through result set
while ($row = $res->fetchRow()) {
echo $row['column_name_a'] . ' ' . $row['column_name_b'] . "<br/>\n";
}

?>


You can insert data with a prepared statement:


<?
require_once "MDB2.php";

$dsn = 'mysql://my_username:@my_password@127.0.0.1/my_db';

$options = array(
'debug' => 2,
'result_buffering' => false,
);

$mdb2 =& MDB2::factory($dsn, $options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}

// Insert
$statement = $mdb2->prepare('INSERT INTO clients VALUES (?, ?)');
$data = array($var1, $var2);
$statement->execute($data);
$statement->free();
?>


These are just the basics...read all the goodness here:

http://pear.php.net/manual/en/package.database.mdb2.php

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels