Independent Digital

Creating and managing dynamic websites using Php, MySql, HTML and Wordpress with a bit of PEAR on the side

Entries Comments



Email This Post    Print This Post   

PEAR:MDB2 tutorial for getting started and using it

18 April, 2007 (13:05) | MySQL, PEAR | By: clive


I thought I’d put together this tutorial for those of you, like me, would like to use something to make life easier when using databases but are somewhat overwhelmed when first dabbling with database abstraction layers such as PEAR:mdb2. Fear not, mdb2 is pretty easy to use once you know how. And that is the point of this tutorial, to show you how to use mdb2 without too much hassle.

This is not a tutorial in the sense that I ramble on and give snippets of code which eventually leaves you totally confused. Rather I have created a package of files covering the main aspects of mdb2. Each file has all the code commented to make understanding the code very easy. So for example the create_db.php file has all the code necessay to create a database. You actually run the code and enter a database name and the database is created. Here’s a sample from the code:

//create the data source name
$dsn = ‘mysql://root:sasha@localhost’;//database name is omitted as it will be added from your input
//do the connection thing
$mdb2 =& MDB2::connect($dsn);
//if there is an error
if (PEAR::isError($mdb2))
{
die($mdb2->getMessage());
}
// loading the Manager module
$mdb2->loadModule(’Manager’);

By working through the code and acompanying comments, you will quickly see how easy it is to use mdb2. Naturally you can tweek the code and use it as you please.

The files in the package cover the following:

  • Creating a database
  • Deleting a database
  • Creating a table
  • Deleting a table
  • Creating an index
  • Deleting an index
  • Creating sequences
  • Deleting sequences
  • Creating constraints
  • Deleting constraints
  • Inserting records
  • Searching records
  • You can get the file package here


    Sorry, there are no related posts but check these out

    Comments

    Comment from ithome
    Time: June 12, 2007, 5:24 pm

    Thanks for the tutorial, good work.

    Comment from Brad
    Time: October 16, 2007, 5:46 am

    I’d like to make a suggestion of possibly making an include file that contains the DSN.

    This way, there’s no hard coded connection details in your examples, and they can be easily configured to play with any DB that the viewer might want to use.